Design Parking System

Easy Topics Company Tags

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 returns true if a matching space is available; otherwise it returns false and 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 <= 1000
  • carType is 1, 2, or 3.
  • At most 1000 calls are made to addCar per object.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.