Problem Statement:-
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 theParkingSystem
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 ofcarType
for the car that wants to get into the parking lot.carType
can be of three kinds: big, medium, or small, which are represented by1
,2
, and3
respectively. A car can only park in a parking space of itscarType
. If there is no space available, returnfalse
, else park the car in that size space and returntrue
.
Link: https://leetcode.com/problems/design-parking-system/description/
Problem Explanation with examples:-
Example 1
Input
["ParkingSystem", "addCar", "addCar", "addCar", "addCar"]
[[1, 1, 0], [1], [2], [3], [1]]
Output
[null, true, true, false, false]
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.
Constraints
0 <= big, medium, small <= 1000
carType
is1
,2
, or3
At most
1000
calls will be made toaddCar
Intuition:-
We can simply have 3 variables to store the number of available spots for each car type.
When a car of a certain type arrives, we check if there is an available spot for that car type and reduce the number of available spots by 1. Return True if there is an available spot and False if there is no available spot.
Solution:-
Initialize the 3 variables b, m, and s with the number of available spots for each car type.
When a car of type 1 arrives: Check if there is an available spot for that car type (if b > 0) and return True. Otherwise, return False. Reduce the number of available spots by 1. Same for car types 2 and 3.
Code:-
JAVA Solution
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) {
boolean ans = big > 0;
big--;
return ans;
} else if (carType == 2) {
boolean ans = medium > 0;
medium--;
return ans;
}
boolean ans = small > 0;
small--;
return ans;
}
}
Python Solution
class ParkingSystem:
def __init__(self, big: int, medium: int, small: int):
self.b = big
self.m = medium
self.s = small
def addCar(self, carType: int) -> bool:
if carType == 1:
ans = self.b > 0
self.b -= 1
return ans
elif carType == 2:
ans = self.m > 0
self.m -= 1
return ans
ans = self.s > 0
self.s -= 1
return ans
Complexity Analysis:-
TIME:-
The time complexity of the addCar
method is O(1). The method performs a constant number of operations regardless of the input size. It simply checks the availability of the corresponding parking spot based on the car type and updates the availability count.
SPACE:-
The space complexity is O(1) as well. The class ParkingSystem
has three member variables (big
, medium
, and small
) that store the availability counts for each type of parking spot. The space usage remains constant regardless of the input size or the number of method calls.