NeetCode #134LC-1603EasyArrays & Hashing
← Back to All Problems#134 · #1603 · Design Parking System(设计停车系统)
📌 Problem Statement & Constraints
Design a parking system with
big, medium and small slots. Implement addCar(carType) returning true if a slot of that size is free (and occupying it), or false otherwise. Constraints: 0 <= big, medium, small <= 1000, 1 <= carType <= 3; at most 1000 calls.💡 Core Algorithmic Approaches
- Keep three counters, one per slot size.
- Index them directly by
carType - 1or store a length-4 array socarTypemaps straight to the slot. - On
addCar, decrement the counter if positive and returntrue; otherwise returnfalse. - This is a pure state-machine warm-up; the only care needed is not to decrement when full.
💻 Benchmark Python3 Implementation
class ParkingSystem:
def __init__(self, big: int, medium: int, small: int):
self.slots = [0, big, medium, small] # index by carType directly
def addCar(self, carType: int) -> bool:
if self.slots[carType] > 0:
self.slots[carType] -= 1
return True
return False⚡ Complexity Deep Dive
⏱️ Time Complexity
O(1) per call.
💾 Space Complexity
O(1): four integers.
⚠️ Interview Pitfalls & Follow-ups
- Decrementing below zero: the
> 0guard is required, and the counter must not go negative. - Using
carType - 1with a length-3 list: both work, but mixing the two conventions is a classic source of off-by-one bugs. Pick one. - Returning the remaining count instead of a boolean: the API returns a boolean.