Sponsored
Sponsored
This approach leverages an array to manage the count of available parking slots for each type of car. The array indices correspond to the car types: index 0 for big cars, index 1 for medium, and index 2 for small. The addCar function decrements the corresponding index if space is available.
Time Complexity: O(1) for each addCar operation as we are directly accessing an array element.
Space Complexity: O(1) as only a fixed-size array is used.
The JavaScript solution employs an array to manage the quantities of the parking slots. The addCar
function decreases the count for a slot upon a successful addition attempt.
This method uses distinct variables to handle each type of car's parking slots. This approach makes the code very clear for small data sets. While this isn't necessarily more efficient than the array method for this particular problem, it offers an alternative for simple systems where explicit clarity is beneficial.
Time Complexity: O(1) for checking and updating.
Space Complexity: O(1) as three variables track the state.
1class ParkingSystem:
2 def __init__(self, big: int, medium: int, small: int):
3 self.big = big
4 self.medium = medium
5 self.small = small
6
7 def addCar(self, carType: int) -> bool:
8 if carType == 1:
9 if self.big > 0: self.big -= 1; return True
10 elif carType == 2:
11 if self.medium > 0: self.medium -= 1; return True
12 elif carType == 3:
13 if self.small > 0: self.small -= 1; return True
14 return False
This Python variant uses distinct attributes for managing parking spaces. Using conditional statements, addCar
checks and updates respective attribute values.