You are tasked with designing a ParkingSystem class for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small. The number of each type of parking space is provided during the initialization of the ParkingSystem object. A method addCar is needed to check if there is a parking space available for a car of specified type, and park the car if there’s a spot. The method should return true if the car is successfully parked, and false otherwise.
Here’s the class definition:
class ParkingSystem:
def __init__(self, big: int, medium: int, small: int):
pass
def addCar(self, carType: int) -> bool:
pass
carType: 1 represents a big car, 2 represents a medium car, and 3 represents a small car.carType provided, check if there are available spaces in the respective parking category.true.false.Here’s the complete implementation of the ParkingSystem class:
class ParkingSystem:
def __init__(self, big: int, medium: int, small: int):
self.big = big
self.medium = medium
self.small = small
def addCar(self, carType: int) -> bool:
if carType == 1:
if self.big > 0:
self.big -= 1
return True
else:
return False
elif carType == 2:
if self.medium > 0:
self.medium -= 1
return True
else:
return False
elif carType == 3:
if self.small > 0:
self.small -= 1
return True
else:
return False
else:
return False # Invalid car type
__init__ method runs in constant time, O(1), since it only sets up initial state variables.addCar method also runs in constant time, O(1), as it only performs a few conditional checks and updates.This solution ensures that adding a car to the parking system is efficient and quick, with a guaranteed O(1) time complexity per operation.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?