# Design Parking System Leetcode Solution

Difficulty Level Easy

## Problem Statement

In this problem, we have to design a parking lot. We have 3 kinds of parking spaces (big, medium and small). All these parking spaces has some fixed number of empty slots initially.
Like, in big type of space, we can place at most b cars. In small type of space, we can place at most s cars and in medium type of space, we can place m cars.

We are given the values b, s and m (maximum limit of all three spaces) in the constructor of the given class. Now, we have to place some cars in these spaces. The cars too are of three types big small and medium. And they can only be placed into their respective parking lot. Like small car can only be placed in small parking lot.

We have to find out if we can place these cars or not. For this purpose we have to create a function addCar(int type) which will return true if the car of this type can be placed or not else this will return false.

### Example

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

Explanation:

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

## Approach

We have to do two task here. One is to use the values given to the constructor. Another is to create function addCar().
Now, think if we have b empty spaces of a big parking lot and we have to place a big car in this. Simply, we will place this car and our empty space drops to b-1.
What if we have no empty spaces for big cars? i.e. when b=0, we can’t place any big car.

Our approach is doing the same. To show the number of empty spaces of each parking slots, we have created three global variables in the given class (let big, small and medium). We are initializing their values with the values given in constructor.
Now, we can process the addCar() request.

Whenever a car of type carType will come, we will check for the number of spaces in the respective parking slot. If the number of spaces would be 0, we can’t place this car. So we will return false. Else we will place this car and will decrease the number of space of that type by 1 and will return true.

## Implementation

### C++ Program for Design Parking System Leetcode Solution

```#include<iostream>
using namespace std;

class ParkingSystem {
public:

int big,medium,small;
ParkingSystem(int big1, int medium1, int small1)
{
big=big1;
medium=medium1;
small=small1;
}

{
if(carType==1){
if(big==0)return false;
big--;
}else if(carType==2){
if(medium==0)return false;
medium--;

}else{
if(small==0)return false;
small--;

}
return true;
}
};

int main()
{
ParkingSystem obj = ParkingSystem(1,1,0);

return 0;
}
```
`true true false false`

### Java Program for Design Parking System Leetcode Solution

```import java.util.*;
import java.lang.*;

class ParkingSystem {
int big,medium,small;
public ParkingSystem(int big, int medium, int small)
{
this.big=big;
this.medium=medium;
this.small=small;
}

{
if(carType==1){
if(big==0)return false;
big--;
}else if(carType==2){
if(medium==0)return false;
medium--;

}else{
if(small==0)return false;
small--;

}
return true;
}
}

class Solution
{
public static void main(String args[])
{
ParkingSystem obj=new ParkingSystem(1,1,0);
}
}```
`true true false false`

## Complexity Analysis for Design Parking System Leetcode Solution

#### Time Complexity

O(1): The algorithm has simple if else tasks. Thus time complexity is O(1).

#### Space Complexity

O(1): no extra space used.

Translate »