Skip to content

Commit f5d52cd

Browse files
committed
Create 0621-task-scheduler.swift
1 parent 7f79c9b commit f5d52cd

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

swift/0621-task-scheduler.swift

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
class Heap {
2+
var heap = [0]
3+
4+
var count: Int {
5+
heap.count - 1
6+
}
7+
8+
var first: Int {
9+
heap[1]
10+
}
11+
12+
func heapify(_ arr: [Int]) {
13+
heap = arr
14+
heap.append(arr[0])
15+
var cur = (heap.count - 1) / 2
16+
while cur > 0 {
17+
var i = cur
18+
while 2 * i < heap.count {
19+
if 2 * i + 1 < heap.count && heap[2 * i + 1] > heap[2 * i] && heap[i] < heap[2 * i + 1] {
20+
let tmp = heap[i]
21+
heap[i] = heap[2 * i + 1]
22+
heap[2 * i + 1] = tmp
23+
i = 2 * i + 1
24+
} else if heap[i] < heap[2 * i] {
25+
let tmp = heap[i]
26+
heap[i] = heap[2 * i]
27+
heap[2 * i] = tmp
28+
i = 2 * i
29+
} else {
30+
break
31+
}
32+
}
33+
cur -= 1
34+
}
35+
}
36+
37+
func push(_ val: Int) {
38+
heap.append(val)
39+
var i = heap.count - 1
40+
while i > 1 && heap[i] > heap[i / 2] {
41+
let tmp = heap[i]
42+
heap[i] = heap[i / 2]
43+
heap[i / 2] = tmp
44+
i = i / 2
45+
}
46+
}
47+
48+
func pop() -> Int? {
49+
if heap.count == 1 {
50+
return nil
51+
}
52+
if heap.count == 2 {
53+
return heap.popLast()
54+
}
55+
let res = heap[1]
56+
heap[1] = heap.removeLast()
57+
var i = 1
58+
while 2 * i < heap.count {
59+
if 2 * i + 1 < heap.count && heap[2 * i + 1] > heap[2 * i] && heap[i] < heap[2 * i + 1] {
60+
let tmp = heap[i]
61+
heap[i] = heap[2 * i + 1]
62+
heap[2 * i + 1] = tmp
63+
i = 2 * i + 1
64+
} else if heap[i] < heap[2 * i] {
65+
let tmp = heap[i]
66+
heap[i] = heap[2 * i]
67+
heap[2 * i] = tmp
68+
i = 2 * i
69+
} else {
70+
break
71+
}
72+
}
73+
return res
74+
}
75+
}
76+
77+
class ListNode {
78+
var val: (Int, Int)
79+
var next: ListNode?
80+
81+
init(_ val: (Int, Int)) {
82+
self.val = val
83+
}
84+
}
85+
86+
class Queue {
87+
var head: ListNode?
88+
var tail: ListNode?
89+
var size = 0
90+
91+
func enqueue(_ val: (Int, Int)) {
92+
let node = ListNode(val)
93+
if head == nil {
94+
head = node
95+
tail = node
96+
} else {
97+
tail?.next = node
98+
tail = tail?.next
99+
}
100+
101+
size += 1
102+
}
103+
104+
func dequeue() -> (Int, Int)? {
105+
if head == nil {
106+
return nil
107+
}
108+
109+
let node = head
110+
head = head?.next
111+
if head == nil {
112+
tail = nil
113+
}
114+
size -= 1
115+
return node?.val
116+
}
117+
}
118+
119+
class Solution {
120+
func leastInterval(_ tasks: [Character], _ n: Int) -> Int {
121+
var count = [Character: Int]()
122+
for t in tasks {
123+
count[t, default: 0] += 1
124+
}
125+
let heap = Heap()
126+
heap.heapify(Array(count.values))
127+
128+
var time = 0
129+
var q = Queue()
130+
while heap.count > 0 || q.size > 0 {
131+
time += 1
132+
if heap.count == 0 {
133+
time = q.head!.val.1
134+
} else {
135+
let cnt = heap.pop()! - 1
136+
if cnt != 0 {
137+
q.enqueue((cnt, time + n))
138+
}
139+
}
140+
if q.size > 0 && q.head!.val.1 == time {
141+
heap.push(q.dequeue()!.0)
142+
}
143+
}
144+
145+
return time
146+
}
147+
}

0 commit comments

Comments
 (0)