-
Notifications
You must be signed in to change notification settings - Fork 7
/
Laptop Rentals.py
122 lines (101 loc) · 3.89 KB
/
Laptop Rentals.py
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
"""
Laptop Rentals ☆
You're given a list of time intervals during which students at a school need a laptop. These time intervals are represented
by pairs of integers [start, end] , where 0 <= start < end . However, start and end don't represent real
times; therefore, they may be greater than 24 .
No two students can use a laptop at the same time, but immediately after a student is done using a laptop, another
student can use that same laptop. For example, if one student rents a laptop during the time interval [0, 2] , another
student can rent the same laptop during any time interval starting with 2.
Write a function that returns the minimum number of laptops that the school needs to rent such that all students will
always have access to a laptop when they need one.
Sample Input
times =
[
[0, 2],
[1, 4],
[4, 6],
[0, 4],
[7, 8],
[9, 11),
[3, 10],
]
Sample Output
3
"""
# SOLUTION 1
# O(nlog(n)) time | O(n) space - where n is the number of times
def laptopRentals(times):
if len(times) == 0:
return 0
usedLaptops = 0
startTimes = sorted([interval[0] for interval in times])
endTimes = sorted([interval[1] for interval in times])
startIterator = 0
endIterator = 0
while startIterator < len(times):
if startTimes[startIterator] >= endTimes[endIterator]:
usedLaptops -= 1
endIterator += 1
usedLaptops += 1
startIterator += 1
return usedLaptops
# SOLUTION 2
# O(nlog(n)) time | O(n) space - where n is the number of times
def laptopRentals(times):
if len(times) == 0:
return 0
times.sort(key=lambda x: x[0])
timesWhenLaptopIsused = [times[0]]
heap = Minheap(timesWhenLaptopIsused)
for idx in range(1, len(times)):
currentInterval = times[idx]
if heap.peek()[1] <= currentInterval[0]:
heap.remove()
heap.insert(currentInterval)
return len(timesWhenLaptopIsused)
class Minheap:
def __init__(self, array):
self.heap = self.buildHeap(array)
# O(n) time 0(1) space
def buildHeap(self, array):
firstParentIdx = (len(array) - 2) // 2
for currentIdx in reversed(range(firstParentIdx + 1)):
self.siftDown(currentIdx, len(array) - 1, array)
return array
# O(log(n)) time | 0(1) space
def siftDown(self, currentIdx, endIdx, heap):
childoneIdx = currentIdx * 2 + 1
while childoneIdx <= endIdx:
childTwoIdx = currentIdx * 2 + 2 if currentIdx * 2 + 2 <= endIdx else -1
if childTwoIdx != -1 and heap[childTwoIdx][1] < heap[childoneIdx][1]:
idxToSwap = childTwoIdx
else:
idxToSwap = childoneIdx
if heap[idxToSwap][1] < heap[currentIdx][1]:
self.swap(currentIdx, idxToSwap, heap)
currentIdx = idxToSwap
childoneIdx = currentIdx * 2 + 1
else:
return
# O(log(n)) time | 0(1) space
def siftUp(self, currentIdx, heap):
parentIdx = (currentIdx - 1) // 2
while currentIdx > 0 and heap[currentIdx][1] < heap[parentIdx][1]:
self.swap(currentIdx, parentIdx, heap)
currentIdx = parentIdx
parentIdx = (currentIdx - 1) // 2
# 0(1) time | 0(1) space
def peek(self):
return self.heap[0]
# O(log(n)) time | 0(1) space
def remove(self):
self.swap(0, len(self.heap) - 1, self.heap)
valueToRemove = self.heap.pop()
self.siftDown(0, len(self.heap) - 1, self.heap)
return valueToRemove
# O(log(n)) time | 0(1) space
def insert(self, value):
self.heap.append(value)
self.siftUp(len(self.heap) - 1, self.heap)
def swap(self, i, j, heap):
heap[i], heap[j] = heap[j], heap[i]