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) { |
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
| /** | |
| * Counts how many zero positions in the array can lead to all elements being reduced to zero | |
| * by moving forward or backward and decrementing non-zero values. | |
| * | |
| * @param {number[]} nums - Array of non-negative integers | |
| * @return {number} - Total number of valid selections from zero positions | |
| */ | |
| var countValidSelections = function (nums) { | |
| // Collect indices where the value is zero | |
| const zeroIndexArr = nums.reduce((acc, cur, idx) => { |
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 {string[]} bank | |
| * @return {number} | |
| */ | |
| // Function to calculate the total number of laser beams between security devices | |
| var numberOfBeams = function(bank) { | |
| // Array to store the count of devices (i.e., '1's) in each non-empty row | |
| let arr = []; | |
| // Variable to accumulate the total number of beams |
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
| /** | |
| * Bank constructor initializes the bank with account balances. | |
| * @param {number[]} balance - Array of initial balances for each account (1-indexed). | |
| */ | |
| var Bank = function(balance) { | |
| // Create an array of accounts with an extra slot at index 0 (unused). | |
| // This allows 1-based indexing for accounts. | |
| this.accounts = Array.from({ length: balance.length + 1 }, (_, i) => balance[i - 1] ?? 0); | |
| // Store the total number of accounts including the unused 0th index. |
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} | |
| */ | |
| // Function to claculate the total amount of money saved over 'n' days | |
| var totalMoney = function(n) { | |
| let total = 0; // Initialize total money saved | |
| let weekDay = 1; // Start from the first day of the week | |
| let weekNumber = 1; // Start from the first week |