Skip to content

Commit 879d751

Browse files
committed
feat: add solutions to lc problem: No.1603
No.1603.Design Parking System
1 parent 3a5ed87 commit 879d751

File tree

6 files changed

+171
-31
lines changed

6 files changed

+171
-31
lines changed

solution/1600-1699/1603.Design Parking System/README.md

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ parkingSystem.addCar(1); // 返回 false ,因为没有空的大车位,唯一
4848

4949
<!-- 这里可写通用的实现逻辑 -->
5050

51-
为每种车维护一个计数器,初始值为车位的数目。此后,每来一辆车,就将对应类型的计数器减 1。当计数器为 0 时,说明车位已满。
51+
**方法一:模拟**
52+
53+
为每种车维护一个计数器,初始值为车位的数目。此后,每来一辆车,就将对应类型的计数器减 `1`。当计数器为 `0` 时,说明车位已满。
54+
55+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
5256

5357
<!-- tabs:start -->
5458

@@ -79,20 +83,17 @@ class ParkingSystem:
7983

8084
```java
8185
class ParkingSystem {
82-
83-
private int[] spaces = new int[3];
86+
private int[] cnt;
8487

8588
public ParkingSystem(int big, int medium, int small) {
86-
spaces[0] = big;
87-
spaces[1] = medium;
88-
spaces[2] = small;
89+
cnt = new int[]{0, big, medium, small};
8990
}
90-
91+
9192
public boolean addCar(int carType) {
92-
if (spaces[carType - 1] <= 0) {
93+
if (cnt[carType] == 0) {
9394
return false;
9495
}
95-
--spaces[carType - 1];
96+
--cnt[carType];
9697
return true;
9798
}
9899
}
@@ -104,6 +105,57 @@ class ParkingSystem {
104105
*/
105106
```
106107

108+
### **C++**
109+
110+
```cpp
111+
class ParkingSystem {
112+
public:
113+
vector<int> cnt;
114+
115+
ParkingSystem(int big, int medium, int small) {
116+
cnt = {0, big, medium, small};
117+
}
118+
119+
bool addCar(int carType) {
120+
if (cnt[carType] == 0) return false;
121+
--cnt[carType];
122+
return true;
123+
}
124+
};
125+
126+
/**
127+
* Your ParkingSystem object will be instantiated and called as such:
128+
* ParkingSystem* obj = new ParkingSystem(big, medium, small);
129+
* bool param_1 = obj->addCar(carType);
130+
*/
131+
```
132+
133+
### **Go**
134+
135+
```go
136+
type ParkingSystem struct {
137+
cnt []int
138+
}
139+
140+
func Constructor(big int, medium int, small int) ParkingSystem {
141+
return ParkingSystem{[]int{0, big, medium, small}}
142+
}
143+
144+
func (this *ParkingSystem) AddCar(carType int) bool {
145+
if this.cnt[carType] == 0 {
146+
return false
147+
}
148+
this.cnt[carType]--
149+
return true
150+
}
151+
152+
/**
153+
* Your ParkingSystem object will be instantiated and called as such:
154+
* obj := Constructor(big, medium, small);
155+
* param_1 := obj.AddCar(carType);
156+
*/
157+
```
158+
107159
### **Rust**
108160

109161
```rust

solution/1600-1699/1603.Design Parking System/README_EN.md

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ parkingSystem.addCar(1); // return false because there is no available slot for
4848

4949
```python
5050
class ParkingSystem:
51+
5152
def __init__(self, big: int, medium: int, small: int):
52-
self.spaces = [big, medium, small]
53+
self.cnt = [0, big, medium, small]
5354

5455
def addCar(self, carType: int) -> bool:
55-
if self.spaces[carType - 1] <= 0:
56+
if self.cnt[carType] == 0:
5657
return False
57-
self.spaces[carType - 1] -= 1
58+
self.cnt[carType] -= 1
5859
return True
5960

6061

@@ -67,20 +68,17 @@ class ParkingSystem:
6768

6869
```java
6970
class ParkingSystem {
70-
71-
private int[] spaces = new int[3];
71+
private int[] cnt;
7272

7373
public ParkingSystem(int big, int medium, int small) {
74-
spaces[0] = big;
75-
spaces[1] = medium;
76-
spaces[2] = small;
74+
cnt = new int[]{0, big, medium, small};
7775
}
78-
76+
7977
public boolean addCar(int carType) {
80-
if (spaces[carType - 1] <= 0) {
78+
if (cnt[carType] == 0) {
8179
return false;
8280
}
83-
--spaces[carType - 1];
81+
--cnt[carType];
8482
return true;
8583
}
8684
}
@@ -92,6 +90,57 @@ class ParkingSystem {
9290
*/
9391
```
9492

93+
### **C++**
94+
95+
```cpp
96+
class ParkingSystem {
97+
public:
98+
vector<int> cnt;
99+
100+
ParkingSystem(int big, int medium, int small) {
101+
cnt = {0, big, medium, small};
102+
}
103+
104+
bool addCar(int carType) {
105+
if (cnt[carType] == 0) return false;
106+
--cnt[carType];
107+
return true;
108+
}
109+
};
110+
111+
/**
112+
* Your ParkingSystem object will be instantiated and called as such:
113+
* ParkingSystem* obj = new ParkingSystem(big, medium, small);
114+
* bool param_1 = obj->addCar(carType);
115+
*/
116+
```
117+
118+
### **Go**
119+
120+
```go
121+
type ParkingSystem struct {
122+
cnt []int
123+
}
124+
125+
func Constructor(big int, medium int, small int) ParkingSystem {
126+
return ParkingSystem{[]int{0, big, medium, small}}
127+
}
128+
129+
func (this *ParkingSystem) AddCar(carType int) bool {
130+
if this.cnt[carType] == 0 {
131+
return false
132+
}
133+
this.cnt[carType]--
134+
return true
135+
}
136+
137+
/**
138+
* Your ParkingSystem object will be instantiated and called as such:
139+
* obj := Constructor(big, medium, small);
140+
* param_1 := obj.AddCar(carType);
141+
*/
142+
```
143+
95144
### **Rust**
96145

97146
```rust
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class ParkingSystem {
2+
public:
3+
vector<int> cnt;
4+
5+
ParkingSystem(int big, int medium, int small) {
6+
cnt = {0, big, medium, small};
7+
}
8+
9+
bool addCar(int carType) {
10+
if (cnt[carType] == 0) return false;
11+
--cnt[carType];
12+
return true;
13+
}
14+
};
15+
16+
/**
17+
* Your ParkingSystem object will be instantiated and called as such:
18+
* ParkingSystem* obj = new ParkingSystem(big, medium, small);
19+
* bool param_1 = obj->addCar(carType);
20+
*/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
type ParkingSystem struct {
2+
cnt []int
3+
}
4+
5+
func Constructor(big int, medium int, small int) ParkingSystem {
6+
return ParkingSystem{[]int{0, big, medium, small}}
7+
}
8+
9+
func (this *ParkingSystem) AddCar(carType int) bool {
10+
if this.cnt[carType] == 0 {
11+
return false
12+
}
13+
this.cnt[carType]--
14+
return true
15+
}
16+
17+
/**
18+
* Your ParkingSystem object will be instantiated and called as such:
19+
* obj := Constructor(big, medium, small);
20+
* param_1 := obj.AddCar(carType);
21+
*/

solution/1600-1699/1603.Design Parking System/Solution.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
class ParkingSystem {
2-
3-
private int[] spaces = new int[3];
2+
private int[] cnt;
43

54
public ParkingSystem(int big, int medium, int small) {
6-
spaces[0] = big;
7-
spaces[1] = medium;
8-
spaces[2] = small;
5+
cnt = new int[]{0, big, medium, small};
96
}
10-
7+
118
public boolean addCar(int carType) {
12-
if (spaces[carType - 1] <= 0) {
9+
if (cnt[carType] == 0) {
1310
return false;
1411
}
15-
--spaces[carType - 1];
12+
--cnt[carType];
1613
return true;
1714
}
1815
}

solution/1600-1699/1603.Design Parking System/Solution.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
class ParkingSystem:
2+
23
def __init__(self, big: int, medium: int, small: int):
3-
self.spaces = [big, medium, small]
4+
self.cnt = [0, big, medium, small]
45

56
def addCar(self, carType: int) -> bool:
6-
if self.spaces[carType - 1] <= 0:
7+
if self.cnt[carType] == 0:
78
return False
8-
self.spaces[carType - 1] -= 1
9+
self.cnt[carType] -= 1
910
return True
1011

1112

0 commit comments

Comments
 (0)