-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path659-split-array-into-consecutive-subsequences.go
62 lines (53 loc) · 1.32 KB
/
659-split-array-into-consecutive-subsequences.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Link: https://leetcode.com/problems/split-array-into-consecutive-subsequences/
import (
"container/heap"
)
type PriorityQueue []int
func (pq *PriorityQueue) Len() int {
return len(*pq)
}
func (pq *PriorityQueue) Swap(i,j int) {
(*pq)[i], (*pq)[j] = (*pq)[j], (*pq)[i]
}
func (pq *PriorityQueue) Less(i, j int) bool {
return (*pq)[i] < (*pq)[j]
}
func (pq *PriorityQueue) Push(k interface{}) {
*pq = append(*pq, k.(int))
}
func (pq *PriorityQueue) Pop() interface{} {
n := len(*pq)
k := (*pq)[n-1]
*pq = (*pq)[:n-1]
return k
}
func isPossible(nums []int) bool {
hmap := make(map[int]*PriorityQueue)
for _, k := range nums {
if pq, ok := hmap[k-1]; ok && len(*pq) > 0 {
minLen := heap.Pop(pq).(int)
if _, ok := hmap[k]; !ok {
hmap[k] = &PriorityQueue{}
}
q, _ := hmap[k]
heap.Push(q, minLen + 1)
} else {
if _, ok := hmap[k]; !ok {
hmap[k] = &PriorityQueue{}
}
q, _ := hmap[k]
heap.Push(q, 1)
}
}
for _, v := range hmap {
if len(*v) == 0 {
continue
}
if heap.Pop(v).(int) < 3 {
return false
}
}
return true
}
// Time complexity: O(NlogN)
// Space complexity: O(N)