File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change 2182
2182
2403|[ Minimum Time to Kill All Monsters] ( ./solutions/2403-minimum-time-to-kill-all-monsters.js ) |Hard|
2183
2183
2404|[ Most Frequent Even Element] ( ./solutions/2404-most-frequent-even-element.js ) |Easy|
2184
2184
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|
2185
2186
2408|[ Design SQL] ( ./solutions/2408-design-sql.js ) |Medium|
2186
2187
2409|[ Count Days Spent Together] ( ./solutions/2409-count-days-spent-together.js ) |Easy|
2187
2188
2410|[ Maximum Matching of Players With Trainers] ( ./solutions/2410-maximum-matching-of-players-with-trainers.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments