-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path621-task-scheduler.go
73 lines (64 loc) · 1.46 KB
/
621-task-scheduler.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
63
64
65
66
67
68
69
70
71
72
73
// Link: https://leetcode.com/problems/task-scheduler/
import (
"container/heap"
)
// SOLUTION USING PRIORITY QUEUE AND TMP QUEUE
type PQueue []int
func (pq *PQueue) Push(x interface{}) {
*pq = append(*pq, x.(int))
}
func (pq *PQueue) Pop() interface{} {
n := len(*pq)
task := (*pq)[n-1]
*pq = (*pq)[:n-1]
return task
}
func (pq PQueue) Less(i,j int) bool {
return pq[i] > pq[j]
}
func (pq PQueue) Len() int {
return len(pq)
}
func (pq PQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
}
func leastInterval(tasks []byte, n int) int {
hmap := make(map[byte]int)
for _, t := range tasks {
if _, ok := hmap[t]; !ok {
hmap[t] = 1
} else {
hmap[t]++
}
}
pq := make(PQueue, 0)
heap.Init(&pq)
buff := make([]int, 0)
for _, v := range hmap {
heap.Push(&pq, v)
}
ans := 0
for pq.Len() > 0 {
k := n + 1
for k > 0 && pq.Len() > 0 {
task := heap.Pop(&pq).(int)
buff = append(buff, task-1)
ans++
k--
}
for len(buff) > 0 {
task := buff[0]
buff = buff[1:]
if task > 0 {
heap.Push(&pq, task)
}
}
if pq.Len() == 0 {
break
}
ans += k
}
return ans
}
// Time complexity: O(NlogN)
// Space complexity: O(N)