Leetcode

Design Parking System

  • Time:O(1)
  • Space:O(1)

C++

class ParkingSystem {
 public:
  ParkingSystem(int big, int medium, int small) {
    count = {big, medium, small};
  }

  bool addCar(int carType) {
    return count[carType - 1]-- > 0;
  }

 private:
  vector<int> count;
};

JAVA

class ParkingSystem {
  public ParkingSystem(int big, int medium, int small) {
    count = new int[] {big, medium, small};
  }

  public boolean addCar(int carType) {
    return count[carType - 1]-- > 0;
  }

  private int[] count;
}

Python

class ParkingSystem:
  def __init__(self, big: int, medium: int, small: int):
    self.count = [big, medium, small]

  def addCar(self, carType: int) -> bool:
    self.count[carType - 1] -= 1
    return self.count[carType - 1] >= 0