add new solutions
This commit is contained in:
parent
d798d5e8c9
commit
886b5e0a8e
34 changed files with 1164 additions and 0 deletions
30
solutions/0/q57/solution.go
Normal file
30
solutions/0/q57/solution.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package q57
|
||||
|
||||
func insert(intervals [][]int, newInterval []int) [][]int {
|
||||
ret := make([][]int, 0, len(intervals)+1)
|
||||
|
||||
i := 0
|
||||
for ; i < len(intervals) && intervals[i][0] <= newInterval[0]; i++ {
|
||||
ret = append(ret, intervals[i])
|
||||
}
|
||||
|
||||
if len(ret) > 0 && ret[len(ret)-1][1] >= newInterval[0] {
|
||||
ret[len(ret)-1][1] = max(ret[len(ret)-1][1], newInterval[1])
|
||||
} else {
|
||||
ret = append(ret, newInterval)
|
||||
}
|
||||
|
||||
for ; i < len(intervals); i++ {
|
||||
last := ret[len(ret)-1]
|
||||
next := intervals[i]
|
||||
if last[1] >= next[0] {
|
||||
last[1] = max(last[1], next[1])
|
||||
} else {
|
||||
ret = append(ret, next)
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
var _ = insert
|
||||
Loading…
Add table
Add a link
Reference in a new issue