Back to solutions
Design Parking System
EasyArraysTrack parking spots of three sizes and admit cars while space remains.
Constraints
- 0 <= big, medium, small <= 1000
- carType in {1, 2, 3}
Three counters
Time O(1) per carSpace O(1)
Store one remaining-count per size. addCar checks the counter for the car's type: if it is positive, decrement it and return true; otherwise return false. Constant time per car.
Key terms
- counter:
- An integer tracking how many spots of a given size are still free.
bool addCar(int carType) {
if (slots[carType] > 0) { slots[carType]--; return true; }
return false;
}Step by step
- Store counts indexed by type: slots[1]=big, slots[2]=medium, slots[3]=small.
- addCar(type): if slots[type] > 0, decrement and return true.
- Otherwise return false (no spot of that size).