LeetCode 1603. Design Parking System

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class ParkingSystem {
private:
int b, m, s;
public:
ParkingSystem(int big, int medium, int small): b(big), m(medium), s(small) {
}
bool addCar(int carType) {
switch (carType) {
case 1:
return b-- > 0;
case 2:
return m-- > 0;
case 3:
return s-- > 0;
}
return false;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class ParkingSystem {
private:
int counts;
public:
ParkingSystem(int big, int medium, int small) {
counts = (small << 20) | (medium << 10) | big;
}
bool addCar(int carType) {
int bits = (carType - 1) * 10;
if ((counts >> bits) & 0b1111111111) {
counts -= 1 << bits;
return true;
}
return false;
}
};