1603. Design Parking System

In this i am going to discuss the problem Design Parking System , 

We need to implement the parkingSystem class , where we have parking spaces for type of car , i.e big , medium , small (which would be given to us as a parameter in the input / constructor class).

Then we are going to add cars,

1 represents big car

2 represents medium car

3 represents small car.

Everytime we add a car , the number of parking spaces avaiable for that type of car reduces by 1. Note in this problem , the cars don’t go out of their parking place once they are parked. So the number of parking spaces always decreases and never increases from the initial values.

To solve this , we can check everytime , the carType using if conditions and return true if there is a place to park the car , i.e there was atleast 1 free parking space for that type of car at the moment we try to add it. Else we can just return 0 / that there is no space avaiable for that car.  

Here is the code

class ParkingSystem {
public:
    int B = 0;
    int M = 0;
    int S = 0;
    ParkingSystem(int big, int medium, int small) {
        B = big;
        M = medium;
        S = small;
    }
    
    bool addCar(int carType) {
        if(carType == 1) {
            B--;
            if(B >= 0) return 1;
        }
        if(carType == 2) {
            M--;
            if(M >= 0) return 1;
        }
        if(carType == 3) {
            S--;
            if(S >= 0) return 1;
        }
        return 0;
    }
};

Leave a Comment