#1603

Design Parking System

newbie · 120 · lc easy +12 · 87.2% accepted · 2,057 likes · top 98%

Description

Design a ParkingSystem for a lot with three space sizes: big, medium, and small. Implement the class:

- ParkingSystem(int big, int medium, int small) initializes the lot with a fixed number of spaces for each size.

- bool addCar(int carType) parks a car in a space of its type (1=big, 2=medium, 3=small) if one is available, returning true, or false if all matching spaces are full.

Example 1:

Input
["ParkingSystem", "addCar", "addCar", "addCar", "addCar"]
[[1, 1, 0], [1], [2], [3], [1]]
Output
[null, true, true, false, false]

Example 2:

Explanation
ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);
parkingSystem.addCar(1); // return true because there is 1 available slot for a big car
parkingSystem.addCar(2); // return true because there is 1 available slot for a medium car
parkingSystem.addCar(3); // return false because there is no available slot for a small car
parkingSystem.addCar(1); // return false because there is no available slot for a big car. It is already occupied.

Code

1
2
3
4
5
6
7
8
9
10
11
12