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 countPartitions = function(nums) { | |
| // Step 1: Compute the total sum of the array. | |
| // We'll use this to quickly calculate the right subarray sum at each partition. | |
| let totalSum = 0; | |
| for (let num of nums) { | |
| totalSum += num; |
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} directions | |
| * @return {number} | |
| */ | |
| var countCollisions = function(directions) { | |
| // Step 1: Convert string to array for easier handling | |
| let arr = directions.split(''); | |
| // Step 2: Remove cars that will never collide | |
| // Trim leading 'L' cars (they move left off the road) |
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
| /** | |
| * Count the number of unique trapezoids that can be formed | |
| * from a set of points on the Cartesian plane. | |
| * | |
| * @param {number[][]} points - Array of [x, y] coordinates | |
| * @return {number} - Number of trapezoids | |
| */ | |
| var countTrapezoids = function(points) { | |
| const t = new Map(); // Tracks lines grouped by slope (normalized) | |
| const v = new Map(); // Tracks lines grouped by raw vector direction |
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[][]} points | |
| * @return {number} | |
| */ | |
| var countTrapezoids = function(points) { | |
| const MOD = 1e9 + 7; | |
| // Step 1: Group points by y-coordinate | |
| let yGroups = new Map(); | |
| for (let [x, y] of points) { |
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 running time for `n` computers | |
| * given a set of batteries with different capacities. | |
| * | |
| * @param {number} n - Number of computers | |
| * @param {number[]} batteries - Array of battery capacities | |
| * @return {number} - Maximum possible running time | |
| */ | |
| var maxRunTime = function(n, batteries) { | |
| // Step 1: Compute the total available power across all batteries |
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 minOperations = function(nums, k) { | |
| // Step 1: Calculate the sum of the array | |
| let sum = 0; | |
| for (let num of nums) { | |
| sum += num; |
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 |
NewerOlder