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:
parent
260cae1010
commit
92c153c768
1 changed files with 27 additions and 0 deletions
27
go/divide-intervals-into-minimum-number-of-groups.go
Normal file
27
go/divide-intervals-into-minimum-number-of-groups.go
Normal 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
|
||||
}
|
Loading…
Reference in a new issue