-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leetcode May Daily Challenge 29.py
41 lines (31 loc) · 1.26 KB
/
Leetcode May Daily Challenge 29.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
class ParkingSystem:
def __init__(self, big: int, medium: int, small: int):
# Parking limit for each type of car
self.big_limit = big
self.medium_limit = medium
self.small_limit = small
# Create an Array to store parked cars
self.parking_array = [None] * (big + medium + small)
def addCar(self, carType: int) -> bool:
# Depending on carType, store the limit for the type of car
if carType == 1:
limit = self.big_limit
elif carType == 2:
limit = self.medium_limit
else:
limit = self.small_limit
# Traverse linearly through the array from the left
count = 0
for i in range(len(self.parking_array)):
# Count the number of cars parked in the system of that type
if self.parking_array[i] == carType:
count += 1
# Stop if the count becomes equal to the limit
if count == limit:
return False
# If the count is less than the limit, then add the car
# to the first available empty slot from the left
if self.parking_array[i] is None:
self.parking_array[i] = carType
return True
return False