This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number} num1 | |
| * @param {number} num2 | |
| * @return {number} | |
| */ | |
| var countOperations = function(num1, num2) { | |
| // initialize a counter to keep track of the number of operations | |
| let operations = 0; | |
| // Continue looping until either num1 or num2 becomes 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Calculates the minimum number of operations to reduce a number `n` to 0 | |
| * using a specific bitwise operation rule: | |
| * - You can flip the lowest 1-bit and all bits to its right. | |
| * | |
| * This function uses a recursive approach based on the properties of Gray code. | |
| * | |
| * @param {number} n - The input number. | |
| * @return {number} - Minimum number of operations to reduce `n` to 0. | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} stations - Initial number of power stations in each city | |
| * @param {number} r - Range of each power station (affects cities within |i - j| <= r) | |
| * @param {number} k - Number of additional power stations that can be built | |
| * @return {number} - Maximum possible minimum power across all cities | |
| */ | |
| var maxPower = function (stations, r, k) { | |
| const n = stations.length; | |
| // Step 1: Build prefix sum array to quickly compute power in a range |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Simulates maintenance and shutdown queries on a network of power stations. | |
| * | |
| * @param {number} c - Number of power stations (1-indexed). | |
| * @param {number[][]} connections - Bidirectional cables between stations. | |
| * @param {number[][]} queries - List of queries: [1, x] for maintenance, [2, x] to shut down. | |
| * @return {number[]} - Results of maintenance queries. | |
| */ | |
| var processQueries = function(c, connections, queries) { | |
| // ----- Union-Find (Disjoint Set Union) Setup ----- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} nums - Input array of numbers | |
| * @param {number} k - Size of the sliding window | |
| * @param {number} x - Number of top elements to sum (based on freq × value) | |
| * @return {number[]} - Array of sums for each window | |
| */ | |
| var findXSum = function(nums, k, x) { | |
| const n = nums.length; | |
| // Shortcut: if k === x, just sum the window directly |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Computes a sliding window sum based on the x most frequent elements. | |
| * | |
| * @param {number[]} nums - The input array of numbers. | |
| * @param {number} k - The size of the sliding window. | |
| * @param {number} x - The number of top frequent elements to include in the sum. | |
| * @return {number[]} - An array of sums for each window. | |
| */ | |
| var findXSum = function(nums, k, x) { | |
| const result = []; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Removes balloons to ensure no two adjacent balloons have the same color, | |
| * minimizing the total removal time. | |
| * | |
| * @param {string} colors - A string where each character represents a balloon's color. | |
| * @param {number[]} neededTime - An array where each element is the time to remove the corresponding balloon. | |
| * @return {number} - The minimum total time to remove balloons to satisfy the condition. | |
| */ | |
| var minCost = function(colors, neededTime) { | |
| let totalTime = 0; // Total time to remove balloons |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Finds the two "sneaky" numbers added to an array that originally contained all numbers from 0 to n-1. | |
| * The final array has length n + 2, and includes two unknown extra numbers. | |
| * | |
| * @param {number[]} nums - Array containing numbers from 0 to n-1 plus two extra unknowns. | |
| * @return {number[]} - The two extra numbers that were added. | |
| */ | |
| var getSneakyNumbers = function (nums) { | |
| const n = nums.length - 2; // Original range was 0 to n-1, so n = nums.length - 2 | |
| let xorAll = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} target | |
| * @return {number} | |
| */ | |
| var minNumberOperations = function(target) { | |
| // Initialize the total number of operations needed | |
| let operations = 0; | |
| // Loop through the target array | |
| for (let i = 0; i < target.length; i++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number} n | |
| * @return {number} | |
| */ | |
| var smallestNumber = function(n) { | |
| // Start with 1 and keep shifting left and OR-ing to build numbers like 1, 3, 7, 15, 31, etc. | |
| let x = 1; | |
| // Keep generating numbers with all bits set until we find one >= n | |
| while (x < n) { |