문제풀이 2021 01 15
2021.01.15 연습
- 사용언어 : java
class ParkingSystem {
int b = 0;
int m = 0;
int s = 0;
public ParkingSystem(int big, int medium, int small) {
b = big;
m = medium;
s = small;
}
public boolean addCar(int carType) {
if (carType == 1 && b > 0) {
b--;
return true;
}
if (carType == 2 && m > 0) {
m--;;
return true;
}
if (carType == 3 && s > 0) {
s--;
return true;
}
return false;
}
}
/**
* Your ParkingSystem object will be instantiated and called as such:
* ParkingSystem obj = new ParkingSystem(big, medium, small);
* boolean param_1 = obj.addCar(carType);
*/
Runtime: 6 ms, faster than 99.78% of Java online submissions for Design Parking System. Memory Usage: 39.7 MB, less than 64.96% of Java online submissions for Design Parking System.
- 다른코드 참고
class ParkingSystem {
private int[] size;
public ParkingSystem(int big, int medium, int small) {
this.size = new int[]{big, medium, small};
}
public boolean addCar(int carType) {
return size[carType-1]-->0;
}
}
// 1.we can take 3 variables to hold count of big,med,small counts. here i have taken array of size 3 instead.
// 2.now just use carType-1 as index of the array and check if value present at that index > 0. if yes return true else false.
// 3.apart from that keep on decrementing value present on carType-1 index. since not more than 1000 calls cannot be made value decremented cannot go out of range.