其他分享
首页 > 其他分享> > 【Leetcode】1603. Design Parking System

【Leetcode】1603. Design Parking System

作者:互联网

题目地址:

https://leetcode.com/problems/design-parking-system/

设计一个停车系统,构造函数给定三个值, b , m , s b,m,s b,m,s分别代表大车、中车和小车的容纳量。还要实现一个addCar方法,参数是车的种类, 1 , 2 , 3 1,2,3 1,2,3分别代表大中小,返回该车是否还有位子停放。

代码如下:

public class ParkingSystem {
    
    private int[] capacity;
    
    public ParkingSystem(int big, int medium, int small) {
        capacity = new int[]{big, medium, small};
    }
    
    public boolean addCar(int carType) {
        if (capacity[carType - 1] == 0) {
            return false;
        }
        
        capacity[carType - 1]--;
        return true;
    }
}

时空复杂度 O ( 1 ) O(1) O(1)。

标签:medium,capacity,int,1603,System,carType,Design,return,public
来源: https://blog.csdn.net/qq_46105170/article/details/112426974