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 | |
| * @return {number} | |
| */ | |
| var maxSubarraySum = function(nums, k) { | |
| let n = nums.length; | |
| // Step 1: Build prefix sums | |
| let prefix = new Array(n + 1).fill(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[][]} grid | |
| * @param {number} k | |
| * @return {number} | |
| */ | |
| var numberOfPaths = function(grid, k) { | |
| const MOD = 1e9 + 7; // modulus for large answers | |
| const m = grid.length; // number of rows | |
| const n = grid[0].length; // number of columns | |
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} k | |
| * @return {number} | |
| */ | |
| var smallestRepunitDivByK = function(k) { | |
| // Step 1: Handle impossible cases | |
| // Any number made only of '1's is odd and not divisible by 2 or 5. | |
| // So if k has a factor of 2 or 5, return -1 immediately. | |
| if (k % 2 === 0 || k % 5 === 0) { | |
| return -1; |
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 | |
| * @return {boolean[]} | |
| */ | |
| var prefixesDivBy5 = function(nums) { | |
| // Result array to store true/false for each prefix | |
| let answer = []; | |
| // We'll keep track of the current number modulo 5 | |
| // This avoids dealing with huge binary numbers 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
| /** | |
| * @param {number[]} nums | |
| * @return {number} | |
| */ | |
| var maxSumDivThree = function(nums) { | |
| // Step 1: Calculate the total sum of the array | |
| let totalSum = nums.reduce((acc, num) => acc + num, 0); | |
| // Step 2: If already divisible by 3, return it | |
| if (totalSum % 3 === 0) return totalSum; |
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 | |
| * @return {number} | |
| */ | |
| var minimumOperations = function(nums) { | |
| // Initialize a counter to keep track of total operations | |
| let operations = 0; | |
| // Loop through each number in the array | |
| for (let num of nums) { |
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 minimum number of integers needed so that | |
| * each interval contains at least two chosen numbers. | |
| * | |
| * @param {number[][]} intervals - Array of [start, end] intervals | |
| * @return {number} - Minimum number of integers chosen | |
| */ | |
| var intersectionSizeTwo = function(intervals) { | |
| // Step 1: Sort intervals | |
| // - Primary: by end ascending (smaller right boundary first) |
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} original | |
| * @return {number} | |
| */ | |
| var findFinalValue = function(nums, original) { | |
| // Step 1: Convert nums into a Set for faster lookups. | |
| // Why? Searching in an array is O(n), but in a Set it's O(1). | |
| let numSet = new Set(nums); |
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[]} bits | |
| * @return {boolean} | |
| */ | |
| var isOneBitCharacter = function(bits) { | |
| // Start from the first bit | |
| let i = 0; | |
| // Traverse until the second-to-last bit | |
| // (because the last bit is always 0, we want to see if it's consumed or not) |
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 | |
| * @return {boolean} | |
| */ | |
| var kLengthApart = function(nums, k) { | |
| // Keep track of the index of the last '1' we saw | |
| let lastOneIndex = -1; | |
| // Loop through the array |