LeetCode 1603: Design Parking System (Capacity Counter Design)
LeetCode 1603DesignArrayToday we solve LeetCode 1603 - Design Parking System.
Source: https://leetcode.com/problems/design-parking-system/
English
Problem Summary
Implement a ParkingSystem class with three capacities: big, medium, and small. Each call to addCar(carType) should park the car if a slot is available for that type and return true; otherwise return false.
Key Insight
This is a direct design problem: each car type is independent. So a fixed array of remaining slots is enough. For carType in {1,2,3}, check remain[carType], decrement when possible, and return the result.
Algorithm
- Store remaining slots in an array indexed by car type.
- Constructor fills the initial capacities.
- addCar(carType): if count is zero, return false.
- Otherwise decrement and return true.
Complexity Analysis
Each addCar call is O(1) time and O(1) extra space. Constructor is also O(1).
Reference Implementations (Java / Go / C++ / Python / JavaScript)
class ParkingSystem {
private final int[] remain = new int[4];
public ParkingSystem(int big, int medium, int small) {
remain[1] = big;
remain[2] = medium;
remain[3] = small;
}
public boolean addCar(int carType) {
if (remain[carType] == 0) {
return false;
}
remain[carType]--;
return true;
}
}type ParkingSystem struct {
remain [4]int
}
func Constructor(big int, medium int, small int) ParkingSystem {
p := ParkingSystem{}
p.remain[1] = big
p.remain[2] = medium
p.remain[3] = small
return p
}
func (p *ParkingSystem) AddCar(carType int) bool {
if p.remain[carType] == 0 {
return false
}
p.remain[carType]--
return true
}class ParkingSystem {
private:
int remain[4]{};
public:
ParkingSystem(int big, int medium, int small) {
remain[1] = big;
remain[2] = medium;
remain[3] = small;
}
bool addCar(int carType) {
if (remain[carType] == 0) {
return false;
}
--remain[carType];
return true;
}
};class ParkingSystem:
def __init__(self, big: int, medium: int, small: int):
self.remain = [0, big, medium, small]
def addCar(self, carType: int) -> bool:
if self.remain[carType] == 0:
return False
self.remain[carType] -= 1
return Truevar ParkingSystem = function(big, medium, small) {
this.remain = [0, big, medium, small];
};
ParkingSystem.prototype.addCar = function(carType) {
if (this.remain[carType] === 0) {
return false;
}
this.remain[carType] -= 1;
return true;
};中文
题目概述
实现一个 ParkingSystem 类,初始化时给出 big、medium、small 三种车位容量。每次调用 addCar(carType):若该类型仍有空位则停车并返回 true,否则返回 false。
核心思路
三类车位互不影响,本质就是三个独立计数器。用长度为 4 的数组按下标 1/2/3 存剩余车位,addCar 时 O(1) 检查并递减即可。
算法步骤
- 用数组保存三种车位剩余数量。
- 构造函数写入初始容量。
- 调用 addCar(carType) 时,若为 0 直接返回 false。
- 否则减一并返回 true。
复杂度分析
每次 addCar 时间复杂度 O(1),额外空间复杂度 O(1);构造函数同样是 O(1)。
多语言参考实现(Java / Go / C++ / Python / JavaScript)
class ParkingSystem {
private final int[] remain = new int[4];
public ParkingSystem(int big, int medium, int small) {
remain[1] = big;
remain[2] = medium;
remain[3] = small;
}
public boolean addCar(int carType) {
if (remain[carType] == 0) {
return false;
}
remain[carType]--;
return true;
}
}type ParkingSystem struct {
remain [4]int
}
func Constructor(big int, medium int, small int) ParkingSystem {
p := ParkingSystem{}
p.remain[1] = big
p.remain[2] = medium
p.remain[3] = small
return p
}
func (p *ParkingSystem) AddCar(carType int) bool {
if p.remain[carType] == 0 {
return false
}
p.remain[carType]--
return true
}class ParkingSystem {
private:
int remain[4]{};
public:
ParkingSystem(int big, int medium, int small) {
remain[1] = big;
remain[2] = medium;
remain[3] = small;
}
bool addCar(int carType) {
if (remain[carType] == 0) {
return false;
}
--remain[carType];
return true;
}
};class ParkingSystem:
def __init__(self, big: int, medium: int, small: int):
self.remain = [0, big, medium, small]
def addCar(self, carType: int) -> bool:
if self.remain[carType] == 0:
return False
self.remain[carType] -= 1
return Truevar ParkingSystem = function(big, medium, small) {
this.remain = [0, big, medium, small];
};
ParkingSystem.prototype.addCar = function(carType) {
if (this.remain[carType] === 0) {
return false;
}
this.remain[carType] -= 1;
return true;
};
Comments