Design Parking System

easy design counting

Problem

A parking lot has spaces of three fixed sizes — big, medium, and small — and the number of spaces of each size (0 to 1000) is given once to the constructor ParkingSystem(big, medium, small). Then addCar(carType) is called up to 1000 times with carType 1 = big, 2 = medium, 3 = small.

A car may only park in a space of exactly its own size. If a matching space is still free, the car takes it and the call returns true; otherwise it returns false.

InputParkingSystem(1, 1, 0); addCar(1), addCar(2), addCar(3), addCar(1)
Output[true, true, false, false]
The first two cars take the only big and only medium space; there are no small spaces at all, and the second big car finds the big space already occupied.

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

    def add_car(self, car_type):
        if self.slots[car_type - 1] > 0:
            self.slots[car_type - 1] -= 1
            return True
        return False
class ParkingSystem {
  constructor(big, medium, small) {
    this.slots = [big, medium, small];
  }

  addCar(carType) {
    if (this.slots[carType - 1] > 0) {
      this.slots[carType - 1]--;
      return true;
    }
    return false;
  }
}
class ParkingSystem {
    private final int[] slots;

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

    public boolean addCar(int carType) {
        if (slots[carType - 1] > 0) {
            slots[carType - 1]--;
            return true;
        }
        return false;
    }
}
class ParkingSystem {
    int slots[3];
public:
    ParkingSystem(int big, int medium, int small) {
        slots[0] = big; slots[1] = medium; slots[2] = small;
    }

    bool addCar(int carType) {
        if (slots[carType - 1] > 0) {
            slots[carType - 1]--;
            return true;
        }
        return false;
    }
};
Time: O(1) per addCar Space: O(1)