1603. Design Parking System - Explanation
Description
Implement ParkingSystem to track available parking spaces of three sizes.
ParkingSystem(big, medium, small)creates a lot with the given number of spaces for each size.addCar(carType)parks one car and returnstrueif a matching space is available; otherwise it returnsfalseand leaves availability unchanged.
Car types 1, 2, and 3 mean big, medium, and small. A car must use its own size of space, even if another size has free spaces. Cars do not leave. The operation list starts with the constructor; its output is null.
Example 1:
Input: ["ParkingSystem","addCar","addCar","addCar","addCar"]
[[0,1,1],[1],[2],[2],[3]]
Output: [null,false,true,false,true]Explanation: The big car cannot park. The first medium car uses the only medium space, so the next medium car fails. The small car uses the available small space.
Example 2:
Input: ["ParkingSystem","addCar","addCar","addCar","addCar"]
[[2,0,0],[3],[1],[1],[1]]
Output: [null,false,true,true,false]Explanation: A small car cannot use either big space and is rejected. The next two big cars park successfully; the third big car finds both spaces occupied.
Example 3:
Input: ["ParkingSystem","addCar","addCar","addCar"]
[[0,0,0],[1],[2],[3]]
Output: [null,false,false,false]Explanation: The constructor creates no spaces. Every parking attempt returns false. The constructor itself contributes null to the output.
Constraints:
0 <= big, medium, small <= 1000carTypeis1,2, or3.- At most 1000 calls are made to
addCarper object.
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- Arrays - Using arrays to store and access data by index
- Index Mapping - Converting between 1-indexed input and 0-indexed arrays
1. Array - I
Intuition
A parking lot has a fixed number of spaces for each car size (big, medium, small). We can represent the available slots using an array of three integers. Since car types are numbered 1, 2, and 3, we map them to array indices 0, 1, and 2 by subtracting 1. When a car arrives, we check if there is room for its type. If so, we decrement the count and allow parking; otherwise, we reject it.
Algorithm
- Initialization: Store the slot counts for big, medium, and small in an array
spacesat indices0,1, and2. - addCar(carType): Check if
spaces[carType - 1] > 0. If yes, decrement that slot count and returntrue. Otherwise, returnfalse.
Time & Space Complexity
- Time complexity:
- time for initialization.
- time for each function call.
- Space complexity:
2. Array - II
Intuition
This is a more concise version of the array approach. Instead of checking before decrementing, we decrement first and then check if the result is non-negative. The logic works because if there were no available slots, the count becomes negative, which we use to indicate failure. This allows the check and update to happen in a single expression.
Algorithm
- Initialization: Store the slot counts for big, medium, and small in an array
spaces. - addCar(carType): Decrement
spaces[carType - 1]and returntrueif the new value is greater than or equal to0, otherwise returnfalse.
Time & Space Complexity
- Time complexity:
- time for initialization.
- time for each function call.
- Space complexity:
Common Pitfalls
Off-by-One Index Error
Car types are 1-indexed (1 = big, 2 = medium, 3 = small), but arrays are 0-indexed. Forgetting to subtract 1 causes an index out of bounds error.
# Wrong: carType is 1-indexed, causes IndexError for carType=3
self.spaces[carType] -= 1
# Correct: convert to 0-indexed
self.spaces[carType - 1] -= 1Decrementing Before Checking Availability
When using the concise approach that decrements first, you must check the value before decrementing (using post-decrement) or check if the result is non-negative. Checking for > 0 after decrementing gives wrong results.
# Wrong: always decrements, even when no space available
self.spaces[carType - 1] -= 1
return self.spaces[carType - 1] > 0 # Returns False when it was 1
# Correct option 1: check first, then decrement
if self.spaces[carType - 1] > 0:
self.spaces[carType - 1] -= 1
return True
return False
# Correct option 2: decrement and check >= 0
self.spaces[carType - 1] -= 1
return self.spaces[carType - 1] >= 0
Sign in to join the discussion