diff --git a/README.md b/README.md index 022ccf05..26278380 100644 --- a/README.md +++ b/README.md @@ -2176,11 +2176,13 @@ 2397|[Maximum Rows Covered by Columns](./solutions/2397-maximum-rows-covered-by-columns.js)|Medium| 2398|[Maximum Number of Robots Within Budget](./solutions/2398-maximum-number-of-robots-within-budget.js)|Hard| 2399|[Check Distances Between Same Letters](./solutions/2399-check-distances-between-same-letters.js)|Easy| +2400|[Number of Ways to Reach a Position After Exactly k Steps](./solutions/2400-number-of-ways-to-reach-a-position-after-exactly-k-steps.js)|Medium| 2401|[Longest Nice Subarray](./solutions/2401-longest-nice-subarray.js)|Medium| 2402|[Meeting Rooms III](./solutions/2402-meeting-rooms-iii.js)|Hard| 2403|[Minimum Time to Kill All Monsters](./solutions/2403-minimum-time-to-kill-all-monsters.js)|Hard| 2404|[Most Frequent Even Element](./solutions/2404-most-frequent-even-element.js)|Easy| 2405|[Optimal Partition of String](./solutions/2405-optimal-partition-of-string.js)|Medium| +2406|[Divide Intervals Into Minimum Number of Groups](./solutions/2406-divide-intervals-into-minimum-number-of-groups.js)|Medium| 2408|[Design SQL](./solutions/2408-design-sql.js)|Medium| 2409|[Count Days Spent Together](./solutions/2409-count-days-spent-together.js)|Easy| 2410|[Maximum Matching of Players With Trainers](./solutions/2410-maximum-matching-of-players-with-trainers.js)|Medium| diff --git a/solutions/2400-number-of-ways-to-reach-a-position-after-exactly-k-steps.js b/solutions/2400-number-of-ways-to-reach-a-position-after-exactly-k-steps.js new file mode 100644 index 00000000..fa0c7186 --- /dev/null +++ b/solutions/2400-number-of-ways-to-reach-a-position-after-exactly-k-steps.js @@ -0,0 +1,49 @@ +/** + * 2400. Number of Ways to Reach a Position After Exactly k Steps + * https://leetcode.com/problems/number-of-ways-to-reach-a-position-after-exactly-k-steps/ + * Difficulty: Medium + * + * You are given two positive integers startPos and endPos. Initially, you are standing at + * position startPos on an infinite number line. With one step, you can move either one position + * to the left, or one position to the right. + * + * Given a positive integer k, return the number of different ways to reach the position endPos + * starting from startPos, such that you perform exactly k steps. Since the answer may be very + * large, return it modulo 109 + 7. + * + * Two ways are considered different if the order of the steps made is not exactly the same. + * + * Note that the number line includes negative integers. + */ + +/** + * @param {number} startPos + * @param {number} endPos + * @param {number} k + * @return {number} + */ +var numberOfWays = function(startPos, endPos, k) { + const MOD = 1e9 + 7; + const dp = new Array(3000).fill().map(() => new Array(k + 1).fill(-1)); + + return helper(startPos, endPos, k); + + function helper(currentPos, targetPos, remainingSteps) { + if (currentPos === targetPos && remainingSteps === 0) { + return 1; + } + if (remainingSteps === 0) { + return 0; + } + + const dpIndex = currentPos + 1000; + if (dp[dpIndex][remainingSteps] !== -1) { + return dp[dpIndex][remainingSteps]; + } + const leftMove = helper(currentPos - 1, targetPos, remainingSteps - 1); + const rightMove = helper(currentPos + 1, targetPos, remainingSteps - 1); + + dp[dpIndex][remainingSteps] = (leftMove + rightMove) % MOD; + return dp[dpIndex][remainingSteps]; + } +}; diff --git a/solutions/2406-divide-intervals-into-minimum-number-of-groups.js b/solutions/2406-divide-intervals-into-minimum-number-of-groups.js new file mode 100644 index 00000000..60b50fb3 --- /dev/null +++ b/solutions/2406-divide-intervals-into-minimum-number-of-groups.js @@ -0,0 +1,39 @@ +/** + * 2406. Divide Intervals Into Minimum Number of Groups + * https://leetcode.com/problems/divide-intervals-into-minimum-number-of-groups/ + * Difficulty: Medium + * + * You are given a 2D integer array intervals where intervals[i] = [lefti, righti] represents + * the inclusive interval [lefti, righti]. + * + * You have to divide the intervals into one or more groups such that each interval is in + * exactly one group, and no two intervals that are in the same group intersect each other. + * + * Return the minimum number of groups you need to make. + * + * Two intervals intersect if there is at least one common number between them. For example, + * the intervals [1, 5] and [5, 8] intersect. + */ + +/** + * @param {number[][]} intervals + * @return {number} + */ +var minGroups = function(intervals) { + const events = []; + for (const [start, end] of intervals) { + events.push([start, 1]); + events.push([end + 1, -1]); + } + + events.sort((a, b) => a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]); + + let activeIntervals = 0; + let result = 0; + for (const [time, change] of events) { + activeIntervals += change; + result = Math.max(result, activeIntervals); + } + + return result; +};