Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size.
Implement the ParkingSystem class:
ParkingSystem(int big, int medium, int small)- Initializes object of the ParkingSystem class. The number of slots for each parking space are given as part of the constructor.bool addCar(int carType)- Checks whether there is a parking space of carType for the car that wants to get into the parking lot. carType can be of three kinds: big, medium, or small, which are represented by 1, 2, and 3 respectively. A car can only park in a parking space of its carType. If there is no space available, return false, else park the car in that size space and return true.
Input: ["ParkingSystem", "addCar", "addCar", "addCar", "addCar"]
[[1, 1, 0], [1], [2], [3], [1]]
Output: [null, true, true, false, false]
Explanation:
ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);
parkingSystem.addCar(1); // return true because there is 1 available slot for a big car
parkingSystem.addCar(2); // return true because there is 1 available slot for a medium car
parkingSystem.addCar(3); // return false because there is no available slot for a small car
parkingSystem.addCar(1); // return false because there is no available slot for a big car
0 <= big, medium, small <= 1000carTypeis 1, 2, or 3- At most 1000 calls will be made to
addCar
I chose a dictionary-based counter approach for the following reasons:
- Simplicity: We only need to track availability counts, not individual parking spots
- Efficiency: O(1) time complexity for both operations
- Scalability: Easy to extend for additional car types
- Clean Code: Dictionary mapping provides readable and maintainable code
- Constructor: Store available space counts in a dictionary mapping car types to counts
- addCar:
- Check if space is available for the given car type
- If available: decrement counter and return
True - If not available: return
Falsewithout changing state
- Time Complexity: O(1) for both constructor and
addCarmethod - Space Complexity: O(1) - only stores 3 integer values regardless of input size
parking_system.py- Complete solution with comprehensive test casesleetcode_solution.py- Clean minimal solution ready for LeetCode submission
The solution includes 4 comprehensive test scenarios:
- LeetCode Original Example - Validates the given example
- Mixed Parking Sequence - Tests realistic mixed car type scenarios
- Boundary Conditions - Tests edge cases with maximum/minimum constraint values
- Repeated Same Type - Stress tests counter management with multiple same-type cars
python parking_system.pyExpected output: All tests pass with detailed execution traces.
def __init__(self, big, medium, small):
self.big_spaces = big
self.medium_spaces = medium
self.small_spaces = small
def addCar(self, carType):
if carType == 1:
if self.big_spaces > 0:
self.big_spaces -= 1
return True
# ... similar for other typesPros: Slightly more explicit Cons: More verbose, harder to extend, requires if-else chains
def __init__(self, big, medium, small):
self.spaces = [0, big, medium, small] # index 0 unusedPros: Compact storage Cons: Less readable, magic indices, potential for index errors
Chosen Approach: Dictionary provides the best balance of readability, efficiency, and maintainability.
- Memory Usage: Minimal - only 3 integer values
- CPU Usage: Constant time operations
- Scalability: Handles up to 1000 spaces per type and 1000 operations efficiently
Ricky Hull
Date: October 14, 2025
LeetCode Problem: 1603 (Easy)
This solution is provided for educational purposes.