class ParkingSystem:
def __init__(self, big: int, medium: int, small: int):
# Index 1: big, 2: medium, 3: small
self.spaces = [0, big, medium, small]
def addCar(self, carType: int) -> bool:
if self.spaces[carType] > 0:
self.spaces[carType] -= 1
return True
return False
class ParkingSystem {
public:
vector<int> spaces;
ParkingSystem(int big, int medium, int small) {
spaces = {0, big, medium, small};
}
bool addCar(int carType) {
if (spaces[carType] > 0) {
spaces[carType]--;
return true;
}
return false;
}
};
class ParkingSystem {
private int[] spaces;
public ParkingSystem(int big, int medium, int small) {
spaces = new int[4];
spaces[1] = big;
spaces[2] = medium;
spaces[3] = small;
}
public boolean addCar(int carType) {
if (spaces[carType] > 0) {
spaces[carType]--;
return true;
}
return false;
}
}
var ParkingSystem = function(big, medium, small) {
this.spaces = [0, big, medium, small];
};
ParkingSystem.prototype.addCar = function(carType) {
if (this.spaces[carType] > 0) {
this.spaces[carType]--;
return true;
}
return false;
};
You are tasked with designing a simple parking system for a parking lot that has three types of parking spaces: big, medium, and small. When the system is initialized, you are given the number of available spots for each type (big
, medium
, small
).
There is one operation: addCar(carType)
, where carType
is 1 (big), 2 (medium), or 3 (small). When a car arrives, you must check if there is an available parking spot of the requested type. If there is, park the car (decrease the count for that type) and return true
; otherwise, return false
.
Key constraints:
The problem is essentially about managing counts of available parking spots for each type. When a car arrives, we need to check if the corresponding type has any spots left.
A brute-force approach might involve maintaining separate lists for each parking type and removing elements as cars park. However, this is unnecessary because we only care about the number of spots left, not their identities.
Optimally, we can use a simple array or list to track the number of available spots for each type. When addCar
is called, we check the count for that type, decrement it if possible, and return the result.
This approach is like having a ticket counter for each parking type: when a car comes, you check if any tickets are left for that type and give one out if possible.
Let's break down the steps for designing the Parking System:
addCar(carType)
is called, check if spaces[carType]
is greater than zero.spaces[carType]
and return true
.false
.This design is simple, efficient, and easy to implement in any programming language.
Let's walk through a sample input:
Initialization: ParkingSystem(1, 1, 0)
(1 big, 1 medium, 0 small)
addCar(1)
: There is 1 big spot. After parking, big = 0. Returns true
.addCar(2)
: There is 1 medium spot. After parking, medium = 0. Returns true
.addCar(3)
: There are 0 small spots. Returns false
.addCar(1)
: There are now 0 big spots left. Returns false
.At each step, the system checks the count for the requested type, updates it if possible, and returns the result.
Brute-force Approach:
addCar
call, since we just check and update an array element.This makes the array-based solution both time and space optimal for this problem.
The Parking System problem is a simple yet instructive example of resource management using counters. By representing each parking type's available spots with an array, we achieve constant-time operations for both checking and updating the state. The solution avoids unnecessary complexity by focusing on the counts rather than the identities of parking spots, resulting in an elegant, efficient design that is easily adaptable to any programming language.