go: add «2406. Divide Intervals Into Minimum Number of Groups»

URL:	https://leetcode.com/problems/divide-intervals-into-minimum-number-of-groups/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-10-13 16:55:53 +02:00
parent 260cae1010
commit 92c153c768
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,27 @@
package main
func minGroups(intervals [][]int) int {
minStart, maxEnd := intervals[0][0], intervals[0][1]
for _, interval := range intervals {
start, end := interval[0], interval[1]
minStart = min(minStart, start)
maxEnd = max(maxEnd, end)
}
hits := make([]int, maxEnd+2)
for _, interval := range intervals {
start, end := interval[0], interval[1]
hits[start]++
hits[end+1]--
}
concurrent, maxConcurrent := 0, 0
for i := minStart; i <= maxEnd; i++ {
concurrent += hits[i]
maxConcurrent = max(maxConcurrent, concurrent)
}
return maxConcurrent
}