Skip to content

Commit ed61374

Browse files
committed
Add solution #2406
1 parent 1661e0a commit ed61374

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,6 +2182,7 @@
21822182
2403|[Minimum Time to Kill All Monsters](./solutions/2403-minimum-time-to-kill-all-monsters.js)|Hard|
21832183
2404|[Most Frequent Even Element](./solutions/2404-most-frequent-even-element.js)|Easy|
21842184
2405|[Optimal Partition of String](./solutions/2405-optimal-partition-of-string.js)|Medium|
2185+
2406|[Divide Intervals Into Minimum Number of Groups](./solutions/2406-divide-intervals-into-minimum-number-of-groups.js)|Medium|
21852186
2408|[Design SQL](./solutions/2408-design-sql.js)|Medium|
21862187
2409|[Count Days Spent Together](./solutions/2409-count-days-spent-together.js)|Easy|
21872188
2410|[Maximum Matching of Players With Trainers](./solutions/2410-maximum-matching-of-players-with-trainers.js)|Medium|
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 2406. Divide Intervals Into Minimum Number of Groups
3+
* https://leetcode.com/problems/divide-intervals-into-minimum-number-of-groups/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 2D integer array intervals where intervals[i] = [lefti, righti] represents
7+
* the inclusive interval [lefti, righti].
8+
*
9+
* You have to divide the intervals into one or more groups such that each interval is in
10+
* exactly one group, and no two intervals that are in the same group intersect each other.
11+
*
12+
* Return the minimum number of groups you need to make.
13+
*
14+
* Two intervals intersect if there is at least one common number between them. For example,
15+
* the intervals [1, 5] and [5, 8] intersect.
16+
*/
17+
18+
/**
19+
* @param {number[][]} intervals
20+
* @return {number}
21+
*/
22+
var minGroups = function(intervals) {
23+
const events = [];
24+
for (const [start, end] of intervals) {
25+
events.push([start, 1]);
26+
events.push([end + 1, -1]);
27+
}
28+
29+
events.sort((a, b) => a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]);
30+
31+
let activeIntervals = 0;
32+
let result = 0;
33+
for (const [time, change] of events) {
34+
activeIntervals += change;
35+
result = Math.max(result, activeIntervals);
36+
}
37+
38+
return result;
39+
};

0 commit comments

Comments
 (0)