-
Notifications
You must be signed in to change notification settings - Fork 11
/
04-merge-interval.rb
468 lines (401 loc) · 12 KB
/
04-merge-interval.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
puts "Merge intervals"
def merge_intervals(intervals)
res = []
return res if intervals.empty?
intervals.sort_by! { |i| i[0] }
prev_start, prev_end = intervals.shift
intervals.each do |current_start, current_end|
if current_start <= prev_end
prev_end = [prev_end, current_end].max
else
res.push([prev_start, prev_end])
prev_start, prev_end = current_start, current_end
end
end
res.push([prev_start, prev_end])
end
def merge_intervals(intervals)
intervals.sort_by! { |i| i[0] }
prev_start, prev_end = intervals.first
res = []
1.upto(intervals.length - 1) do |idx|
cur_start, cur_end = intervals[idx]
if cur_start <= prev_end
prev_end = [cur_end, prev_end].max
else
res << [prev_start, prev_end]
prev_start, prev_end = cur_start, cur_end
end
end
res << [prev_start, prev_end]
end
p merge_intervals [[1, 4], [2, 5], [7, 9]]
p merge_intervals [[6, 7], [2, 4], [5, 9]]
p merge_intervals [[1, 4], [2, 6], [3, 5]]
puts "Insert Interval"
def insert_interval(intervals, interval)
merge_intervals(intervals << interval)
end
def insert_interval(intervals, interval)
intervals = merge_intervals(intervals)
res = []
intervals.each_with_index do |(i_start, i_end), idx|
if i_end < interval[0]
res << [i_start, i_end]
elsif i_start > interval[1]
res << interval
return res + intervals[idx..(intervals.length - 1)]
else
interval = [[i_start, interval[0]].min, [i_end, interval[1]].max]
end
end
res << interval if res.empty? || res.last[1] < interval[0]
res
end
p insert_interval [[1, 3], [5, 7], [8, 12]], [4, 6]
p insert_interval [[1, 3], [5, 7], [8, 12]], [4, 10]
p insert_interval [[2, 3], [5, 7]], [1, 4]
p insert_interval [[2, 3], [5, 7]], [-11, 40]
p insert_interval [[2, 5], [6, 7], [8, 9]], [0, 1]
p "2nd attempt"
def insert_interval(intervals, interval)
intervals = merge_intervals(intervals)
int_start, int_end = interval
res = []
0.upto(intervals.length - 1) do |idx|
cur_start, cur_end = intervals[idx]
if cur_start > int_end
return res + [int_start, int_end] + intervals[idx..-1]
elsif cur_end < int_start
res << [cur_start, cur_end]
else
int_start, int_end = [int_start, cur_start].min, [int_end, cur_end].max
end
end
res << [int_start, int_end] if res.empty? || res.last.last < int_start
res
end
p insert_interval [[1, 3], [5, 7], [8, 12]], [4, 6]
p insert_interval [[1, 3], [5, 7], [8, 12]], [4, 10]
p insert_interval [[2, 3], [5, 7]], [1, 4]
p insert_interval [[2, 3], [5, 7]], [-11, 40]
p insert_interval [[2, 5], [6, 7], [8, 9]], [0, 1]
puts "Interval intersection"
def find_intersection(i1, i2)
idx1, idx2 = 0, 0
res = []
while idx1 < i1.length && idx2 < i2.length
if i1[idx1].last < i2[idx2].first
idx1 += 1
elsif i2[idx2].last < i1[idx1].first
idx2 += 1
else
int_start, int_end = [i1[idx1].first, i2[idx2].first].max, [i1[idx1].last, i2[idx2].last].min
res << [int_start, int_end]
if i1[idx1].last < i2[idx2].last
idx1 += 1
else
idx2 += 1
end
end
end
res
end
p find_intersection [[1, 3], [5, 6], [7, 9]], [[2, 3], [5, 7]]
p find_intersection arr1 = [[1, 3], [5, 7], [9, 12]], arr2 = [[5, 10]]
def find_intersection(i1, i2)
res = []
i1.sort_by! { |i| i[0] }
i2.sort_by! { |i| i[0] }
idx1, idx2 = 0, 0
while idx1 < i1.length && idx2 < i2.length
if i1[idx1][1] < i2[idx2][0]
idx1 += 1
elsif i2[idx2][1] < i1[idx1][0]
idx2 += 1
else
res << [[i1[idx1][0], i2[idx2][0]].max, [i1[idx1][1], i2[idx2][1]].min]
if i1[idx1][1] > i2[idx2][1]
idx2 += 1
else
idx1 += 1
end
end
end
res
end
p find_intersection [[1, 3], [5, 6], [7, 9]], [[2, 3], [5, 7]]
p find_intersection arr1 = [[1, 3], [5, 7], [9, 12]], arr2 = [[5, 10]]
puts "Conflicting appointments"
def can_attend_all(appointments)
appointments.sort_by! { |a| a[0] }
1.upto(appointments.length - 1).all? do |idx|
appointments[idx - 1].last <= appointments[idx].first
end
end
p can_attend_all [[1, 4], [2, 5], [7, 9]]
p can_attend_all [[6, 7], [2, 4], [8, 12]]
p can_attend_all [[4, 5], [2, 3], [3, 6]]
def can_attend_all(appointments)
appointments.sort_by! { |a| a[0] }
prev_end = appointments[0][1]
1.upto(appointments.length - 1) do |idx|
current_start, current_end = appointments[idx]
return false if current_start < prev_end
prev_end = current_end
end
true
end
def can_attend_all(appointments)
appointments.sort_by! { |a| a[0] }
1.upto(appointments.length - 1) do |idx|
return false if appointments[idx][0] < appointments[idx - 1][1]
end
true
end
p can_attend_all [[1, 4], [2, 5], [7, 9]]
p can_attend_all [[6, 7], [2, 4], [8, 12]]
p can_attend_all [[4, 5], [2, 3], [3, 6]]
puts "Minimum meeting rooms: with segment tree"
class SegmentTree
class Node < Struct.new(:start, :finish, :val, :left, :right)
end
attr_accessor :root
def initialize(start, finish)
self.root = Node.new(start, finish, 0)
end
def add(start, finish, val, node = root)
if node.start == start && node.finish == finish
node.val += val
else
mid = (node.start + node.finish) / 2
node.left ||= Node.new(node.start, mid, 0)
node.right ||= Node.new(mid, node.finish, 0)
if finish <= mid
add(start, finish, val, node.left)
elsif start >= mid
add(start, finish, val, node.right)
else
add(start, mid, val, node.left)
add(mid, finish, val, node.right)
end
end
end
def max_path_to_leaf(node = root)
return 0 if node.nil?
node.val + [max_path_to_leaf(node.left), max_path_to_leaf(node.right)].max
end
end
def minimum_meeting_rooms(intervals)
return 0 if intervals.empty?
min, max = 1.0 / 0, -1.0 / 0
intervals.each do |start, finish|
min = start if start < min
max = finish if finish > max
end
st = SegmentTree.new(min, max)
intervals.each do |start, finish|
st.add(start, finish, 1)
end
st.max_path_to_leaf
end
p minimum_meeting_rooms [[1, 4], [2, 5], [7, 9]]
p minimum_meeting_rooms [[6, 7], [2, 4], [8, 12]]
p minimum_meeting_rooms [[1, 4], [2, 3], [3, 6]]
p minimum_meeting_rooms [[4, 5], [2, 3], [2, 4], [3, 5]]
puts "Minimum meeting rooms: with heap"
class Heap
attr_accessor :arr, :comparator
def initialize(&comparator)
self.arr = []
self.comparator = comparator
end
def insert(elem)
arr << elem
heapify_up(arr.size - 1)
end
def top
return nil if empty?
arr.first
end
def remove_top
return nil if empty?
swap(0, arr.length - 1)
top = arr.pop
heapify_down(0)
top
end
def empty?
arr.empty?
end
def size
arr.size
end
private
def compare(idx1, idx2)
self.comparator[arr[idx1], arr[idx2]]
end
def parent(idx)
(idx - 1) / 2
end
def left(idx)
2 * idx + 1
end
def right(idx)
2 * idx + 2
end
def swap(idx, swap_idx)
arr[idx], arr[swap_idx] = arr[swap_idx], arr[idx]
end
def heapify_down(idx)
top_idx = idx
left_idx = left(top_idx)
right_idx = right(top_idx)
if left_idx < arr.size && compare(left_idx, top_idx)
top_idx = left_idx
end
if right_idx < arr.size && compare(right_idx, top_idx)
top_idx = right_idx
end
if top_idx != idx
swap(idx, top_idx)
heapify_down(top_idx)
end
end
def heapify_up(idx)
return if idx.zero?
parent_idx = parent(idx)
if compare(idx, parent_idx)
swap(idx, parent_idx)
heapify_up(parent_idx)
end
end
end
def minimum_meeting_rooms(intervals)
intervals.sort_by! { |start, _| start }
min_rooms, heap = 0, Heap.new { |finish1, finish2| finish1 < finish2 }
intervals.each do |start, finish|
while heap.top && heap.top <= start
heap.remove_top
end
heap.insert(finish)
min_rooms = heap.size if heap.size > min_rooms
end
min_rooms
end
p minimum_meeting_rooms [[1, 4], [2, 5], [7, 9]]
p minimum_meeting_rooms [[6, 7], [2, 4], [8, 12]]
p minimum_meeting_rooms [[1, 4], [2, 3], [3, 6]]
p minimum_meeting_rooms [[4, 5], [2, 3], [2, 4], [3, 5]]
p minimum_meeting_rooms [[6, 17], [8, 9], [11, 12], [6, 9]]
puts "Maximum cpu load: with segment tree"
def max_cpu_load(loads)
min_time, max_time = 1.0 / 0, -1.0 / 0
loads.each do |start, finish, _|
min_time = start if start < min_time
max_time = finish if finish > max_time
end
sg = SegmentTree.new(min_time, max_time)
loads.each do |start, finish, load|
sg.add(start, finish, load)
end
sg.max_path_to_leaf
end
p max_cpu_load [[1, 4, 3], [2, 5, 4], [7, 9, 6]]
p max_cpu_load [[6, 7, 10], [2, 4, 11], [8, 12, 15]]
p max_cpu_load [[1, 4, 2], [2, 4, 1], [3, 6, 5]]
puts "Maximum cpu load: with heap"
def max_cpu_load(loads)
current_load, max_load = 0, 0
loads.sort_by! { |l| l[0] }
heap = Heap.new { |(finish1, _), (finish2, _)| finish1 < finish2 }
loads.each do |start, finish, load|
while heap.top && heap.top[0] <= start
current_load -= heap.remove_top[1]
end
heap.insert([finish, load])
current_load += load
max_load = current_load if current_load > max_load
end
max_load
end
p max_cpu_load [[1, 4, 3], [2, 5, 4], [7, 9, 6]]
p max_cpu_load [[6, 7, 10], [2, 4, 11], [8, 12, 15]]
p max_cpu_load [[1, 4, 2], [2, 4, 1], [3, 6, 5]]
puts "Employee free time"
def find_free_times(hours)
res = []
start_heap = Heap.new do |(emp_idx1, hours_idx1), (emp_idx2, hours_idx2)|
hours[emp_idx1][hours_idx1][0] < hours[emp_idx2][hours_idx2][0]
end
finish_heap = Heap.new { |finish1, finish2| finish1 < finish2 }
0.upto(hours.length - 1) do |idx|
start_heap.insert([idx, 0])
end
while !start_heap.empty?
emp_idx, hours_idx = start_heap.remove_top
start, finish = hours[emp_idx][hours_idx]
if hours_idx < hours[emp_idx].length - 1
start_heap.insert([emp_idx, hours_idx + 1])
end
prev_finish = nil
while finish_heap.top && finish_heap.top <= start
prev_finish = finish_heap.remove_top
end
if prev_finish && finish_heap.empty? && prev_finish != start
res << [prev_finish, start]
end
finish_heap.insert(finish)
end
res
end
p find_free_times [[[1, 3], [5, 6]], [[2, 3], [6, 8]]]
p find_free_times [[[1, 3], [9, 12]], [[2, 4]], [[6, 8]]]
p find_free_times [[[1, 3]], [[2, 4]], [[3, 5], [7, 9]]]
def find_free_times(hours)
res = []
working_heap = Heap.new { |finish1, finish2| finish1 < finish2 }
available_heap = Heap.new do |(worker_idx1, interval_idx1), (worker_idx2, interval_idx2)|
hours[worker_idx1][interval_idx1].first < hours[worker_idx2][interval_idx2].first
end
0.upto(hours.length - 1) do |worker_idx|
available_heap.insert([worker_idx, 0])
end
while !available_heap.empty?
worker_idx, interval_idx = available_heap.remove_top
start_time, finish_time = hours[worker_idx][interval_idx]
while !working_heap.empty? && working_heap.top <= start_time
last_finished = working_heap.remove_top
end
if working_heap.empty? && last_finished && start_time > last_finished
res << [last_finished, start_time]
end
working_heap.insert(finish_time)
available_heap.insert([worker_idx, interval_idx + 1]) if interval_idx < hours[worker_idx].length - 1
end
res
end
def find_free_times(hours)
res = []
start_heap = Heap.new do |(emp_idx1, int_idx1), (emp_idx2, int_idx2)|
hours[emp_idx1][int_idx1].first < hours[emp_idx2][int_idx2].first
end
0.upto(hours.length - 1) do |emp_idx|
start_heap.insert([emp_idx, 0]) unless hours[emp_idx].empty?
end
if !start_heap.empty?
first_emp_idx, first_int_idx = start_heap.top
prev_end = hours[first_emp_idx][first_int_idx].last
while !start_heap.empty?
emp_idx, int_idx = start_heap.remove_top
cur_start, cur_end = hours[emp_idx][int_idx]
res << [prev_end, cur_start] if cur_start > prev_end
prev_end = cur_end if cur_end > prev_end
start_heap.insert([emp_idx, int_idx + 1]) if int_idx < hours[emp_idx].length - 1
end
end
res
end
p find_free_times [[[1, 3], [5, 6]], [[2, 3], [6, 8]]]
p find_free_times [[[1, 3], [9, 12]], [[2, 4]], [[6, 8]]]
p find_free_times [[[1, 3]], [[2, 4]], [[3, 5], [7, 9]]]