Skip to content

Commit c78a518

Browse files
authored
Update 2406.divide-intervals-into-minimum-number-of-groups.js
1 parent 80740af commit c78a518

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

2406.divide-intervals-into-minimum-number-of-groups.js

+28
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,31 @@ var minGroups = function(intervals) {
1818
}
1919
return res
2020
};
21+
22+
// another
23+
24+
/**
25+
* @param {number[][]} intervals
26+
* @return {number}
27+
*/
28+
const minGroups = function(intervals) {
29+
const hash = {}
30+
for(const [s, e] of intervals) {
31+
if(hash[s] == null) hash[s] = 0
32+
if(hash[e + 1] == null) hash[e + 1] = 0
33+
hash[s]++
34+
hash[e + 1]--
35+
}
36+
const keys = Object.keys(hash)
37+
keys.sort((a, b) => a - b)
38+
const n = keys.length
39+
40+
const arr = Array(n).fill(0)
41+
arr[0] = hash[keys[0]]
42+
let res = arr[0]
43+
for(let i = 1; i < n; i++) {
44+
arr[i] = hash[keys[i]] + arr[i - 1]
45+
res = Math.max(res, arr[i])
46+
}
47+
return res
48+
};

0 commit comments

Comments
 (0)