Design Parking System (LeetCode 1603): Solution and Explanation
Simulate a parking lot with a fixed number of big, medium and small spaces, where each car can only take a space of its own size.
Intuition
A car of one size can never use a space of another size, so the lot is really three independent lots.
Nothing ever leaves, and which space a car takes does not matter. The only fact that affects future answers is how many spaces of each size are still free.
So the whole design is three counters. Putting them in an array where index 1, 2, 3 match the car types lets carType select its counter directly, with no branching.
Approach: Counter per size
- In the constructor, store
slots = [0, big, medium, small]. - In
addCar(carType), ifslots[carType]is 0, return false. - Otherwise decrement
slots[carType]and return true.
Step-by-step example
ParkingSystem(1, 1, 0); addCar(1), addCar(2), addCar(3), addCar(1)
- slots = [0, 1, 1, 0].
- addCar(1): slots[1] = 1, so park the big car. slots[1] = 0, return true.
- addCar(2): slots[2] = 1, park. slots[2] = 0, return true.
- addCar(3): slots[3] = 0, there are no small spaces. Return false.
- addCar(1): slots[1] = 0 now, the big space is taken. Return false. Output: [null,true,true,false,false].
Code: Python, Java, C++
1class ParkingSystem:
2 def __init__(self, big: int, medium: int, small: int):
3 self.slots = [0, big, medium, small]
4
5 def addCar(self, carType: int) -> bool:
6 if self.slots[carType] == 0:
7 return False
8 self.slots[carType] -= 1
9 return True1class ParkingSystem {
2 private int[] slots;
3
4 public ParkingSystem(int big, int medium, int small) {
5 slots = new int[] {0, big, medium, small};
6 }
7
8 public boolean addCar(int carType) {
9 if (slots[carType] == 0) {
10 return false;
11 }
12 slots[carType]--;
13 return true;
14 }
15}1class ParkingSystem {
2 int slots[4];
3
4public:
5 ParkingSystem(int big, int medium, int small) {
6 slots[0] = 0; slots[1] = big; slots[2] = medium; slots[3] = small;
7 }
8
9 bool addCar(int carType) {
10 if (slots[carType] == 0) {
11 return false;
12 }
13 slots[carType]--;
14 return true;
15 }
16};Time and space complexity
O(1) per call
Each call reads and possibly decrements one array entry.
O(1)
Three counters, no matter how many spaces or cars there are.
Other approaches
Three named fields (time O(1) per call, space O(1))
Keep big, medium and small as separate fields and pick one with a switch or if chain. Same cost, a little more code.
Explicit space objects (time O(capacity) per call, space O(big + medium + small))
Model each space as an object with an occupied flag and scan for a free one. Useful if cars could leave a specific space, but O(capacity) per call here.
Common mistakes
- Indexing a 3-element array with carType directly, which is 1-based. Either pad index 0 or subtract 1.
- Letting a small car take a medium or big space. The problem forbids parking in any other size.
- Decrementing before the check, which drives the counter negative and later returns true wrongly.
Frequently asked questions
Why not track each parking space?
Cars never leave and spaces of one size are interchangeable, so only the count of free spaces matters.
What if a size has zero spaces?
Its counter starts at 0, so every car of that type gets false immediately.
How would you support cars leaving?
Add a removeCar(carType) that increments the counter, capped at the original capacity. The counters still capture everything.