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) { |
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 | |
| * @param {number} k | |
| * @param {number} numOperations | |
| * @return {number} | |
| */ | |
| var maxFrequency = function (nums, k, numOperations) { | |
| // Sort the array to enable binary search and range grouping | |
| nums.sort((a, b) => a - b); |
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 - The input array of integers | |
| * @param {number} k - The maximum value that can be added/subtracted in one operation | |
| * @param {number} numOperations - The total number of operations allowed | |
| * @return {number} - The maximum frequency of any element after operations | |
| */ | |
| var maxFrequency = function(nums, k, numOperations) { | |
| const n = nums.length; | |
| // Sort the array to enable binary search and range grouping |
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[]} operations | |
| * @return {number} | |
| */ | |
| var finalValueAfterOperations = function(operations) { | |
| // Step 1: Initialize the variable X to 0 | |
| let X = 0; | |
| // Step 2: Loop through each operation in the array | |
| for (let i = 0; i < operations.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 {string} s | |
| * @param {number} a | |
| * @param {number} b | |
| * @return {string} | |
| */ | |
| var findLexSmallestString = function(s, a, b) { | |
| // Set to keep track of visited strings to avoid revisiting the same state | |
| const visited = new Set(); |
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 maximum number of partitions after performing operations | |
| * that allow up to `k` distinct characters per partition. | |
| * | |
| * @param {string} s - Input string consisting of lowercase letters. | |
| * @param {number} k - Maximum number of distinct characters allowed per partition. | |
| * @return {number} - Maximum number of partitions achievable. | |
| */ | |
| var maxPartitionsAfterOperations = function(s, k) { | |
| const n = s.length; |