-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0621-task-scheduler.swift
147 lines (132 loc) · 3.38 KB
/
0621-task-scheduler.swift
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
class Heap {
var heap = [0]
var count: Int {
heap.count - 1
}
var first: Int {
heap[1]
}
func heapify(_ arr: [Int]) {
heap = arr
heap.append(arr[0])
var cur = (heap.count - 1) / 2
while cur > 0 {
var i = cur
while 2 * i < heap.count {
if 2 * i + 1 < heap.count && heap[2 * i + 1] > heap[2 * i] && heap[i] < heap[2 * i + 1] {
let tmp = heap[i]
heap[i] = heap[2 * i + 1]
heap[2 * i + 1] = tmp
i = 2 * i + 1
} else if heap[i] < heap[2 * i] {
let tmp = heap[i]
heap[i] = heap[2 * i]
heap[2 * i] = tmp
i = 2 * i
} else {
break
}
}
cur -= 1
}
}
func push(_ val: Int) {
heap.append(val)
var i = heap.count - 1
while i > 1 && heap[i] > heap[i / 2] {
let tmp = heap[i]
heap[i] = heap[i / 2]
heap[i / 2] = tmp
i = i / 2
}
}
func pop() -> Int? {
if heap.count == 1 {
return nil
}
if heap.count == 2 {
return heap.popLast()
}
let res = heap[1]
heap[1] = heap.removeLast()
var i = 1
while 2 * i < heap.count {
if 2 * i + 1 < heap.count && heap[2 * i + 1] > heap[2 * i] && heap[i] < heap[2 * i + 1] {
let tmp = heap[i]
heap[i] = heap[2 * i + 1]
heap[2 * i + 1] = tmp
i = 2 * i + 1
} else if heap[i] < heap[2 * i] {
let tmp = heap[i]
heap[i] = heap[2 * i]
heap[2 * i] = tmp
i = 2 * i
} else {
break
}
}
return res
}
}
class ListNode {
var val: (Int, Int)
var next: ListNode?
init(_ val: (Int, Int)) {
self.val = val
}
}
class Queue {
var head: ListNode?
var tail: ListNode?
var size = 0
func enqueue(_ val: (Int, Int)) {
let node = ListNode(val)
if head == nil {
head = node
tail = node
} else {
tail?.next = node
tail = tail?.next
}
size += 1
}
func dequeue() -> (Int, Int)? {
if head == nil {
return nil
}
let node = head
head = head?.next
if head == nil {
tail = nil
}
size -= 1
return node?.val
}
}
class Solution {
func leastInterval(_ tasks: [Character], _ n: Int) -> Int {
var count = [Character: Int]()
for t in tasks {
count[t, default: 0] += 1
}
let heap = Heap()
heap.heapify(Array(count.values))
var time = 0
var q = Queue()
while heap.count > 0 || q.size > 0 {
time += 1
if heap.count == 0 {
time = q.head!.val.1
} else {
let cnt = heap.pop()! - 1
if cnt != 0 {
q.enqueue((cnt, time + n))
}
}
if q.size > 0 && q.head!.val.1 == time {
heap.push(q.dequeue()!.0)
}
}
return time
}
}