-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1834_single_threaded_cpu.rb
89 lines (69 loc) · 1.9 KB
/
1834_single_threaded_cpu.rb
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
# frozen_string_literal: true
# https://leetcode.com/problems/single-threaded-cpu/
# @param {Integer[][]} tasks
# @return {Integer[]}
def get_order(tasks)
sorted_tasks = tasks.each_with_index.map { |t, idx| [t[0], t[1], idx] }.sort_by { |et, _, _| et }
current_time = 0
result = []
i = 0
heap = []
while i < sorted_tasks.size || !heap.empty?
while i < sorted_tasks.size && sorted_tasks[i][0] <= current_time
insert_heap(heap, [sorted_tasks[i][1], sorted_tasks[i][2]])
i += 1
end
if heap.empty? && i < sorted_tasks.size
current_time = sorted_tasks[i][0]
while i < sorted_tasks.size && sorted_tasks[i][0] <= current_time
insert_heap(heap, [sorted_tasks[i][1], sorted_tasks[i][2]])
i += 1
end
end
next if heap.empty?
pt, idx = extract_min(heap)
result << idx
current_time += pt
end
result
end
private
# @param {Integer[]} heap
# @param {Integer} element
# @return {Void}
def insert_heap(heap, element)
heap << element
index = heap.size - 1
while index.positive?
parent = (index - 1) / 2
break unless compare(element, heap[parent])
heap[index], heap[parent] = heap[parent], heap[index]
index = parent
end
end
# @param {Integer[]} heap
# @return {Integer}
def extract_min(heap)
return if heap.empty?
min = heap[0]
last = heap.pop
unless heap.empty?
heap[0] = last
index = 0
loop do
left = 2 * index + 1
right = 2 * index + 2
smallest = index
smallest = left if left < heap.size && compare(heap[left], heap[smallest])
smallest = right if right < heap.size && compare(heap[right], heap[smallest])
break if smallest == index
heap[index], heap[smallest] = heap[smallest], heap[index]
index = smallest
end
end
min
end
# @param {Integer} a
# @param {Integer} b
# @return {Integer}
def compare(a, b) = a[0] < b[0] || (a[0] == b[0] && a[1] < b[1])