Similar Problems
Similar Problems not available
Design Parking System - Leetcode Solution
Companies:
LeetCode: Design Parking System Leetcode Solution
Difficulty: Easy
Topics: design simulation
The Design Parking System problem on leetcode can be solved using object-oriented programming principles. The objective of this problem is to design a system that allows parking of cars in different parking slots.
To solve this problem, we can create a class called ParkingSystem that has three parameters, namely big, medium, and small, representing the number of slots for each type of car. The class should also have a method called park that takes a standard input of the car size and returns a boolean true if there is an available parking slot of that size and false if there is no space available.
Here's the detailed solution for this problem:
Step 1: Create a class called "ParkingSystem" and define the constructor that takes 3 parameters, representing the number of slots for each car size.
class ParkingSystem {
int big, medium, small;
public:
ParkingSystem(int big, int medium, int small) {
this->big = big;
this->medium = medium;
this->small = small;
}
};
Step 2: Define the park method that takes an integer representing the size of the car and returns true if there is an available parking slot of that size and false otherwise.
bool park(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;
}
Step 3: The park method first checks if the input carType is equal to 1. If it is, it checks if there is an available slot for a big car. If there is, it decrements the big variable by 1 and returns true. If there is no available slot, it returns false. Similarly, for carType 2 and 3, it checks for medium and small slots, respectively.
Step 4: Testing the class using sample inputs.
int main() {
ParkingSystem* obj = new ParkingSystem(1, 1, 0);
cout << obj->park(1) << endl; // prints true
cout << obj->park(2) << endl; // prints true
cout << obj->park(3) << endl; // prints false
cout << obj->park(1) << endl; // prints false
return 0;
}
This implementation designates each incoming car to a specific type of parking slot and returns true if there are any cars still available in that type of slot. The above implementation takes O(1) time complexity for parking a car and O(1) memory complexity.
Design Parking System Solution Code
1