Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1603
Browse files Browse the repository at this point in the history
No.1603.Design Parking System
  • Loading branch information
yanglbme committed Sep 9, 2022
1 parent 3a5ed87 commit 879d751
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 31 deletions.
70 changes: 61 additions & 9 deletions solution/1600-1699/1603.Design Parking System/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ parkingSystem.addCar(1); // 返回 false ,因为没有空的大车位,唯一

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

为每种车维护一个计数器,初始值为车位的数目。此后,每来一辆车,就将对应类型的计数器减 1。当计数器为 0 时,说明车位已满。
**方法一:模拟**

为每种车维护一个计数器,初始值为车位的数目。此后,每来一辆车,就将对应类型的计数器减 `1`。当计数器为 `0` 时,说明车位已满。

时间复杂度 $O(1)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -79,20 +83,17 @@ class ParkingSystem:

```java
class ParkingSystem {

private int[] spaces = new int[3];
private int[] cnt;

public ParkingSystem(int big, int medium, int small) {
spaces[0] = big;
spaces[1] = medium;
spaces[2] = small;
cnt = new int[]{0, big, medium, small};
}

public boolean addCar(int carType) {
if (spaces[carType - 1] <= 0) {
if (cnt[carType] == 0) {
return false;
}
--spaces[carType - 1];
--cnt[carType];
return true;
}
}
Expand All @@ -104,6 +105,57 @@ class ParkingSystem {
*/
```

### **C++**

```cpp
class ParkingSystem {
public:
vector<int> cnt;

ParkingSystem(int big, int medium, int small) {
cnt = {0, big, medium, small};
}

bool addCar(int carType) {
if (cnt[carType] == 0) return false;
--cnt[carType];
return true;
}
};

/**
* Your ParkingSystem object will be instantiated and called as such:
* ParkingSystem* obj = new ParkingSystem(big, medium, small);
* bool param_1 = obj->addCar(carType);
*/
```
### **Go**
```go
type ParkingSystem struct {
cnt []int
}
func Constructor(big int, medium int, small int) ParkingSystem {
return ParkingSystem{[]int{0, big, medium, small}}
}
func (this *ParkingSystem) AddCar(carType int) bool {
if this.cnt[carType] == 0 {
return false
}
this.cnt[carType]--
return true
}
/**
* Your ParkingSystem object will be instantiated and called as such:
* obj := Constructor(big, medium, small);
* param_1 := obj.AddCar(carType);
*/
```

### **Rust**

```rust
Expand Down
71 changes: 60 additions & 11 deletions solution/1600-1699/1603.Design Parking System/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ parkingSystem.addCar(1); // return false because there is no available slot for

```python
class ParkingSystem:

def __init__(self, big: int, medium: int, small: int):
self.spaces = [big, medium, small]
self.cnt = [0, big, medium, small]

def addCar(self, carType: int) -> bool:
if self.spaces[carType - 1] <= 0:
if self.cnt[carType] == 0:
return False
self.spaces[carType - 1] -= 1
self.cnt[carType] -= 1
return True


Expand All @@ -67,20 +68,17 @@ class ParkingSystem:

```java
class ParkingSystem {

private int[] spaces = new int[3];
private int[] cnt;

public ParkingSystem(int big, int medium, int small) {
spaces[0] = big;
spaces[1] = medium;
spaces[2] = small;
cnt = new int[]{0, big, medium, small};
}

public boolean addCar(int carType) {
if (spaces[carType - 1] <= 0) {
if (cnt[carType] == 0) {
return false;
}
--spaces[carType - 1];
--cnt[carType];
return true;
}
}
Expand All @@ -92,6 +90,57 @@ class ParkingSystem {
*/
```

### **C++**

```cpp
class ParkingSystem {
public:
vector<int> cnt;

ParkingSystem(int big, int medium, int small) {
cnt = {0, big, medium, small};
}

bool addCar(int carType) {
if (cnt[carType] == 0) return false;
--cnt[carType];
return true;
}
};

/**
* Your ParkingSystem object will be instantiated and called as such:
* ParkingSystem* obj = new ParkingSystem(big, medium, small);
* bool param_1 = obj->addCar(carType);
*/
```
### **Go**
```go
type ParkingSystem struct {
cnt []int
}
func Constructor(big int, medium int, small int) ParkingSystem {
return ParkingSystem{[]int{0, big, medium, small}}
}
func (this *ParkingSystem) AddCar(carType int) bool {
if this.cnt[carType] == 0 {
return false
}
this.cnt[carType]--
return true
}
/**
* Your ParkingSystem object will be instantiated and called as such:
* obj := Constructor(big, medium, small);
* param_1 := obj.AddCar(carType);
*/
```

### **Rust**

```rust
Expand Down
20 changes: 20 additions & 0 deletions solution/1600-1699/1603.Design Parking System/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class ParkingSystem {
public:
vector<int> cnt;

ParkingSystem(int big, int medium, int small) {
cnt = {0, big, medium, small};
}

bool addCar(int carType) {
if (cnt[carType] == 0) return false;
--cnt[carType];
return true;
}
};

/**
* Your ParkingSystem object will be instantiated and called as such:
* ParkingSystem* obj = new ParkingSystem(big, medium, small);
* bool param_1 = obj->addCar(carType);
*/
21 changes: 21 additions & 0 deletions solution/1600-1699/1603.Design Parking System/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
type ParkingSystem struct {
cnt []int
}

func Constructor(big int, medium int, small int) ParkingSystem {
return ParkingSystem{[]int{0, big, medium, small}}
}

func (this *ParkingSystem) AddCar(carType int) bool {
if this.cnt[carType] == 0 {
return false
}
this.cnt[carType]--
return true
}

/**
* Your ParkingSystem object will be instantiated and called as such:
* obj := Constructor(big, medium, small);
* param_1 := obj.AddCar(carType);
*/
13 changes: 5 additions & 8 deletions solution/1600-1699/1603.Design Parking System/Solution.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
class ParkingSystem {

private int[] spaces = new int[3];
private int[] cnt;

public ParkingSystem(int big, int medium, int small) {
spaces[0] = big;
spaces[1] = medium;
spaces[2] = small;
cnt = new int[]{0, big, medium, small};
}

public boolean addCar(int carType) {
if (spaces[carType - 1] <= 0) {
if (cnt[carType] == 0) {
return false;
}
--spaces[carType - 1];
--cnt[carType];
return true;
}
}
Expand Down
7 changes: 4 additions & 3 deletions solution/1600-1699/1603.Design Parking System/Solution.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
class ParkingSystem:

def __init__(self, big: int, medium: int, small: int):
self.spaces = [big, medium, small]
self.cnt = [0, big, medium, small]

def addCar(self, carType: int) -> bool:
if self.spaces[carType - 1] <= 0:
if self.cnt[carType] == 0:
return False
self.spaces[carType - 1] -= 1
self.cnt[carType] -= 1
return True


Expand Down

0 comments on commit 879d751

Please sign in to comment.