Design Parking System — Counter-Based Slot Management
Advertisement
Problem
Design a parking system:
- Constructor:
ParkingSystem(big, medium, small)— number of spaces per type addCar(carType)— park car if space available (1=big, 2=medium, 3=small), return bool
Solutions
Python
class ParkingSystem:
def __init__(self, big: int, medium: int, small: int):
self.spaces = [0, big, medium, small]
def addCar(self, carType: int) -> bool:
if self.spaces[carType] == 0:
return False
self.spaces[carType] -= 1
return True
JavaScript
class ParkingSystem {
constructor(big, medium, small) { this.s = [0, big, medium, small]; }
addCar(t) { return this.s[t]-- > 0; }
}
Java
class ParkingSystem {
int[] s;
public ParkingSystem(int big, int medium, int small) { s=new int[]{0,big,medium,small}; }
public boolean addCar(int t) { return s[t]-- > 0; }
}
C++
class ParkingSystem {
int s[4];
public:
ParkingSystem(int b, int m, int sm) { s[1]=b; s[2]=m; s[3]=sm; }
bool addCar(int t) { return s[t]-- > 0; }
};
C
int spaces[4];
void init(int b, int m, int sm){ spaces[1]=b; spaces[2]=m; spaces[3]=sm; }
int addCar(int t){ return spaces[t]-- > 0; }
Advertisement