-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1834-SingleThreadedCPU.go
More file actions
211 lines (192 loc) · 8.73 KB
/
1834-SingleThreadedCPU.go
File metadata and controls
211 lines (192 loc) · 8.73 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
// 1834. Single-Threaded CPU
// You are given n tasks labeled from 0 to n - 1 represented by a 2D integer array tasks,
// where tasks[i] = [enqueueTimei, processingTimei] means that the ith task will be available to process at enqueueTimei
// and will take processingTimei to finish processing.
// You have a single-threaded CPU that can process at most one task at a time and will act in the following way:
// 1. If the CPU is idle and there are no available tasks to process, the CPU remains idle.
// 2. If the CPU is idle and there are available tasks, the CPU will choose the one with the shortest processing time.
// If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.
// 3. Once a task is started, the CPU will process the entire task without stopping.
// 4. The CPU can finish a task then start a new one instantly.
// Return the order in which the CPU will process the tasks.
// Example 1:
// Input: tasks = [[1,2],[2,4],[3,2],[4,1]]
// Output: [0,2,3,1]
// Explanation: The events go as follows:
// - At time = 1, task 0 is available to process. Available tasks = {0}.
// - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}.
// - At time = 2, task 1 is available to process. Available tasks = {1}.
// - At time = 3, task 2 is available to process. Available tasks = {1, 2}.
// - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}.
// - At time = 4, task 3 is available to process. Available tasks = {1, 3}.
// - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}.
// - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}.
// - At time = 10, the CPU finishes task 1 and becomes idle.
// Example 2:
// Input: tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]]
// Output: [4,3,2,0,1]
// Explanation: The events go as follows:
// - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}.
// - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}.
// - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}.
// - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}.
// - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}.
// - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}.
// - At time = 40, the CPU finishes task 1 and becomes idle.
// Constraints:
// tasks.length == n
// 1 <= n <= 10^5
// 1 <= enqueueTimei, processingTimei <= 10^9
import "fmt"
import "sort"
import "container/heap"
// import "github.com/emirpasic/gods/v2/queues/priorityqueue"
// type task struct {
// enqueueTime, processingTime, index int
// }
// func getOrder(tasks [][]int) []int {
// list := make([]task, len(tasks))
// for i, ints := range tasks {
// list[i] = task{ints[0], ints[1], i}
// }
// slices.SortFunc(list, func(a, b task) int {
// return a.enqueueTime - b.enqueueTime
// })
// pq := priorityqueue.NewWith[task](func(x, y task) int {
// if x.processingTime != y.processingTime {
// return x.processingTime - y.processingTime
// } else {
// return x.index - y.index
// }
// })
// res := make([]int, 0, len(tasks))
// for time := 0; len(list) > 0 || !pq.Empty(); {
// if pq.Empty() {
// time = max(time, list[0].enqueueTime)
// }
// for len(list) > 0 && list[0].enqueueTime <= time {
// pq.Enqueue(list[0])
// list = list[1:]
// }
// value, _ := pq.Dequeue()
// res = append(res, value.index)
// time += value.processingTime
// }
// return res
// }
type Vertex struct {
Position, Time int
}
type PriorityQueue []Vertex
func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool {
if pq[i].Time == pq[j].Time { return pq[i].Position < pq[j].Position }
return pq[i].Time < pq[j].Time
}
func (pq PriorityQueue) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] }
func (pq *PriorityQueue) Push(x any) { *pq = append(*pq, x.(Vertex)) }
func (pq *PriorityQueue) Pop() any {
n := len(*pq)
res := (*pq)[n-1]
*pq = (*pq)[:n-1]
return res
}
func getOrder(tasks [][]int) []int {
for i := range tasks {
tasks[i] = append(tasks[i], i)
}
sort.Slice(tasks, func(i, j int) bool {
return tasks[i][0] < tasks[j][0]
})
res, pq := []int{}, PriorityQueue{}
index, time := 0, tasks[0][0]
for index < len(tasks) || len(pq) > 0 {
for index < len(tasks) && time >= tasks[index][0] {
heap.Push(&pq, Vertex{ Position:tasks[index][2], Time: tasks[index][1] })
index++
}
if len(pq) > 0 {
v := heap.Pop(&pq).(Vertex)
time += v.Time
res = append(res, v.Position)
} else {
time = tasks[index][0]
}
}
return res
}
type Task struct {
Index, Enqueue,Processing int
}
type MinHeap []Task
func (h MinHeap) Len() int { return len(h) }
func (h MinHeap) Less(i, j int) bool {
// Sort by processing time, then by index if processing times are equal
if h[i].Processing == h[j].Processing { return h[i].Index < h[j].Index }
return h[i].Processing < h[j].Processing
}
func (h MinHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *MinHeap) Push(x interface{}) { *h = append(*h, x.(Task)) }
func (h *MinHeap) Pop() interface{} {
old := *h
res := old[len(old) - 1]
*h = old[:len(old) - 1]
return res
}
func getOrder1(tasks [][]int) []int {
arr := []Task{}
for i, task := range tasks {
arr = append(arr, Task{ Index: i, Enqueue: task[0], Processing: task[1], })
}
sort.Slice(arr, func(i, j int) bool { // Sort tasks by their enqueue time
return arr[i].Enqueue < arr[j].Enqueue
})
mnh := &MinHeap{}
heap.Init(mnh)
res, now, index := []int{}, 0, 0
for len(res) < len(arr) {
for index < len(arr) && arr[index].Enqueue <= now { // Add all tasks that have become available by the current time
heap.Push(mnh, arr[index])
index++
}
if mnh.Len() == 0 { // If no tasks are available, jump to the next available task's enqueue time
now = arr[index].Enqueue
} else { // Process the task with the shortest processing time
task := heap.Pop(mnh).(Task)
res = append(res, task.Index)
now += task.Processing
}
}
return res
}
func main() {
// Example 1:
// Input: tasks = [[1,2],[2,4],[3,2],[4,1]]
// Output: [0,2,3,1]
// Explanation: The events go as follows:
// - At time = 1, task 0 is available to process. Available tasks = {0}.
// - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}.
// - At time = 2, task 1 is available to process. Available tasks = {1}.
// - At time = 3, task 2 is available to process. Available tasks = {1, 2}.
// - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}.
// - At time = 4, task 3 is available to process. Available tasks = {1, 3}.
// - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}.
// - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}.
// - At time = 10, the CPU finishes task 1 and becomes idle.
fmt.Println(getOrder([][]int{{1,2},{2,4},{3,2},{4,1}})) // [0,2,3,1]
// Example 2:
// Input: tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]]
// Output: [4,3,2,0,1]
// Explanation: The events go as follows:
// - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}.
// - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}.
// - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}.
// - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}.
// - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}.
// - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}.
// - At time = 40, the CPU finishes task 1 and becomes idle.
fmt.Println(getOrder([][]int{{7,10},{7,12},{7,5},{7,4},{7,2}})) // [4,3,2,0,1]
fmt.Println(getOrder1([][]int{{1,2},{2,4},{3,2},{4,1}})) // [0,2,3,1]
fmt.Println(getOrder1([][]int{{7,10},{7,12},{7,5},{7,4},{7,2}})) // [4,3,2,0,1]
}