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 |
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 next "beautiful" number greater than the given input. | |
| * A beautiful number is defined as one where each digit appears exactly as many times as its value. | |
| * For example, 22 is beautiful because digit 2 appears twice. | |
| * | |
| * @param {number} n - The input number. | |
| * @return {number} - The next beautiful number greater than n. | |
| */ | |
| var nextBeautifulNumber = function(n) { | |
| // Precomputed list of beautiful numbers in ascending order. |
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} s | |
| * @return {boolean} | |
| */ | |
| var hasSameDigits = function(s) { | |
| // Convert the input string into an array of digits (as numbers) | |
| let digits = s.split('').map(Number); | |
| // Repeat the transformation until only two digits remain | |
| while (digits.length > 2) { |