This is the 24th day of my participation in the August Text Challenge.More challenges in August

Design the parking system

Please design a parking system for a parking lot. There are three different sizes of parking Spaces: large, small and medium, with a fixed number of Spaces for each size.

Implement the ParkingSystem class:

ParkingSystem(int big, int medium, int small) initializes the ParkingSystem class with three parameters corresponding to the number of each parking space. Bool addCar(int carType) Checks whether there is a parking space corresponding to carType. Cartypes come in three types: large, medium, and small, denoted by the numbers 1, 2, and 3, respectively. A car can only park in the parking space corresponding to the carType. Return false if there is no empty parking space, otherwise park the car in the parking space and return true.

Example 1:

Input: [” ParkingSystem addCar “, “”,” addCar “, “addCar”, “addCar”] [[1, 1, 0], [1], [2], [3], [1]] output: [null, true, true, false, false]

ParkingSystem ParkingSystem = new ParkingSystem(1, 1, 0); parkingSystem.addCar(1); Parkingsystem.addcar (2); parkingSystem.addcar (2); Parkingsystem.addcar (3); parkingSystem.addcar (3); // Return false because there are no empty car bits parkingSystem.addcar (1); // Return false because there is no empty parking space and the only parking space is already occupied

Their thinking

There are three kinds of parking Spaces: large, medium and small. Every time addCar(carType) is called, we need to add a car to the parking space of the corresponding carType. If there is no space left for the carType, it means that the car cannot park. All we need to do is determine whether we can stop each time we call addCar.

The number of cars in the parking space is increasing

Three variables big,medium and small are used to maintain the number of remaining Spaces of various types of vehicles respectively. When addCar(carType) is used every time, the number of parking Spaces of the corresponding type is -1. If the number of remaining seats of the current vehicle is == 0, then it means that the car cannot be parked any more, return False; Otherwise, if the stop is allowed, return True.

,

code

    class ParkingSystem {

        int big,medium,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)
                return --big>=0;
            else if(carType==2)
                return --medium>=0;
            else return --small>=0; }}/** * Your ParkingSystem object will be instantiated and called as such: * ParkingSystem obj = new ParkingSystem(big, medium, small); * boolean param_1 = obj.addCar(carType); * /
Copy the code