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 countTriples = function(n) { | |
| // Initialize a counter to keep track of valid triples | |
| let count = 0; | |
| // Loop through all possible values of 'a' | |
| for (let a = 1; a <= n; a++) { |
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} low | |
| * @param {number} high | |
| * @return {number} | |
| */ | |
| var countOdds = function(low, high) { | |
| // Step 1: Think about what we want: | |
| // We need to count how many odd numbers exist between low and high (inclusive). | |
| // Step 2: Use math instead of looping: |
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 ways to partition nums into contiguous segments | |
| * such that in each segment, max - min <= k. | |
| * | |
| * @param {number[]} nums - Input array of integers | |
| * @param {number} k - Maximum allowed difference between max and min in a segment | |
| * @return {number} - Number of valid partitions modulo 1e9+7 | |
| */ | |
| var countPartitions = function (nums, k) { | |
| const MOD = 1e9 + 7; |
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 ways to partition nums into contiguous segments | |
| * such that in each segment, max - min <= k. | |
| * | |
| * @param {number[]} nums - Input array of integers | |
| * @param {number} k - Maximum allowed difference between max and min in a segment | |
| * @return {number} - Number of valid partitions modulo 1e9+7 | |
| */ | |
| var countPartitions = function (nums, k) { | |
| const MOD = 1e9 + 7; |
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; |
NewerOlder