Leetcode 1603. Design Parking System
Leetcode Problem 1603 - Design Parking System
You are required to design a parking system for a parking lot. The parking lot has different slots for different sizes of cars:
big: cars that take big slots.medium: cars that take medium slots.small: cars that take small slots.At the start, the parking lot contains a certain number of slots for each size. You need to provide functionality to add a car of different sizes into the parking lot. If there is still a place in the slot for the car size, it is parked successfully and true is returned. Otherwise, false is returned.
Implement the ParkingSystem class:
ParkingSystem(int big, int medium, int small) - initializes the object with big, medium, and small slots.bool addCar(int carType) - checks whether there is some space available for the car type carType. carType can be one of three types:
1: represents a big car2: represents a medium car3: represents a small carcarType will always be in the set {1, 2, 3}?#include <iostream>
class ParkingSystem {
private:
int big;
int medium;
int small;
public:
ParkingSystem(int big, int medium, int small) : big(big), medium(medium), small(small) {}
bool addCar(int carType) {
if (carType == 1) {
if (big > 0) {
big--;
return true;
}
} else if (carType == 2) {
if (medium > 0) {
medium--;
return true;
}
} else if (carType == 3) {
if (small > 0) {
small--;
return true;
}
}
return false;
}
};
// Example of usage
int main() {
ParkingSystem* parkingSystem = new ParkingSystem(1, 1, 0);
std::cout << parkingSystem->addCar(1) << std::endl; // returns true because there is 1 available slot for a big car
std::cout << parkingSystem->addCar(2) << std::endl; // returns true because there is 1 available slot for a medium car
std::cout << parkingSystem->addCar(3) << std::endl; // returns false because there is no available slot for a small car
std::cout << parkingSystem->addCar(1) << std::endl; // returns false because there is no available slot for a big car, it is already occupied
delete parkingSystem;
return 0;
}
big, medium, and small cars as member variables.addCar function, check the carType:
carType is 1, decrement the big slot count if it’s greater than 0 and return true.carType is 2, decrement the medium slot count if it’s greater than 0 and return true.carType is 3, decrement the small slot count if it’s greater than 0 and return true.false.addCar function is O(1), as it involves a few constant-time checks and comparisons.ParkingSystem class is O(1), as it uses a fixed amount of space to store the slot counts for each car type.The solution provided is efficient and meets the problem requirements effectively.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?