Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size.
Implement the ParkingSystem
class:
ParkingSystem(int big, int medium, int small)
Initializes object of the ParkingSystem
class. The number of slots for each parking space are given as part of the constructor.bool addCar(int carType)
Checks whether there is a parking space of carType
for the car that wants to get into the parking lot. carType
can be of three kinds: big, medium, or small, which are represented by 1
, 2
, and 3
respectively. A car can only park in a parking space of its carType
. If there is no space available, return false
, else park the car in that size space and return true
.For example:
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.
How would you implement the ParkingSystem
class to efficiently handle these operations?
Design a parking system for a parking lot with three types of parking spaces: big, medium, and small. The system should initialize with a fixed number of slots for each size and allow cars to park based on their type. Implement the ParkingSystem
class with the following methods:
ParkingSystem(int big, int medium, int small)
: Initializes the object with the number of slots for each parking space size.bool addCar(int carType)
: Checks if a parking space of carType
is available. carType
can be 1 (big), 2 (medium), or 3 (small). If space is available, park the car and return true
; otherwise, return false
.A straightforward approach is to use three separate counters to keep track of the available slots for each car type. During initialization, the counters are set to the initial capacity of each type of parking space. The addCar
method decrements the appropriate counter if space is available and returns true. Otherwise, it returns false.
class ParkingSystem {
private int big;
private int medium;
private int small;
public ParkingSystem(int big, int medium, int small) {
this.big = big;
this.medium = medium;
this.small = small;
}
public boolean addCar(int carType) {
if (carType == 1) {
if (big > 0) {
big--;
return true;
} else {
return false;
}
} else if (carType == 2) {
if (medium > 0) {
medium--;
return true;
} else {
return false;
}
} else {
if (small > 0) {
small--;
return true;
} else {
return false;
}
}
}
}
ParkingSystem
(constructor): O(1)addCar
: O(1)O(1) - constant space is used to store the counts.
The optimal solution is essentially the same as the naive solution since the problem constraints are simple, and the number of car types is fixed. We can slightly optimize the code by using an array to store the counts, making the logic more concise.
class ParkingSystem {
private int[] spaces;
public ParkingSystem(int big, int medium, int small) {
spaces = new int[4]; // Index 0 is unused to match carType numbering
spaces[1] = big;
spaces[2] = medium;
spaces[3] = small;
}
public boolean addCar(int carType) {
if (spaces[carType] > 0) {
spaces[carType]--;
return true;
} else {
return false;
}
}
}
ParkingSystem
(constructor): O(1)addCar
: O(1)O(1) - constant space is used to store the counts.
big
, medium
, or small
. The problem statement specifies that these values will be non-negative, so no explicit handling is needed.carType
is not 1, 2, or 3. The problem statement specifies that carType
will always be one of these values, so no explicit handling is needed.addCar
exhaust all available spaces. The solution correctly handles this by returning false
when there is no space available.