forked from super30admin/Design-5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParkingLot.py
64 lines (43 loc) · 1.61 KB
/
ParkingLot.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
class ParkingLot:
# TC: O(log(N*M))
# SC: O(N*M)
# N --> Number of Floors
# M --> Number of Spots at each floor
def __init__(self, maxFloors, spotsPerFloor):
import heapq
self.heap = []
heapq.heapify(self.heap)
self.maxFloors = maxFloors
self.spotsPerFloor = spotsPerFloor
def park(self):
self.getNextAvailable()
def unpark(self, floor, spot):
self.addParkingSpots(floor, spot)
def getNextAvailable(self):
if len(self.heap) == 0:
raise ValueError("Full Parking Lot.")
return heapq.heappop(self.heap)
def addParkingSpots(self, floor, spot):
if (floor > self.maxFloors):
raise ValueError("Floor input is greater then max allowed.")
if (spot > self.spotsPerFloor):
raise ValueError("Spot input is greater then max spots allowed per floor.")
currParkinSpot = ParkingSpot(floor, spot)
heapq.heappush(self.heap, currParkinSpot)
class ParkingSpot:
def __init__(self, floor, spot):
self.floor = floor
self.spot = spot
def __lt__(self, other):
if (self.floor == other.floor):
return self.spot < other.spot
else:
return self.floor < other.floor
def getFloor(self):
return self.floor
def getSpot(self):
return self.spot
def setFloor(self, floor):
self.floor = floor
def setSpot(self, spot):
self.spot = spot