Skip to content

Commit 4a511eb

Browse files
authored
feat: update lc problems (#4116)
1 parent 8ac4a26 commit 4a511eb

File tree

10 files changed

+61
-142
lines changed

10 files changed

+61
-142
lines changed

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

+15-15
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ parkingSystem.addCar(1); // 返回 false ,因为没有空的大车位,唯一
6666

6767
### 方法一:模拟
6868

69-
为每种车维护一个计数器,初始值为车位的数目。此后,每来一辆车,就将对应类型的计数器减 `1`。当计数器为 `0` 时,说明车位已满。
69+
我们用一个长度为 $4$ 的数组 $\textit{cnt}$ 来表示停车场中每种车位的数量,其中 $\textit{cnt}[1]$, $\textit{cnt}[2]$, $\textit{cnt}[3]$ 分别表示大车位、中车位、小车位的数量。
70+
71+
在初始化时,我们将 $\textit{cnt}[1]$, $\textit{cnt}[2]$, $\textit{cnt}[3]$ 分别初始化为大车位、中车位、小车位的数量。
72+
73+
每次停车时,我们检查停车场中是否有对应车位,如果没有则返回 $\textit{false}$,否则将对应车位的数量减一,并返回 $\textit{true}$。
7074

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

@@ -175,17 +179,17 @@ func (this *ParkingSystem) AddCar(carType int) bool {
175179

176180
```ts
177181
class ParkingSystem {
178-
private count: [number, number, number];
182+
private cnt: [number, number, number, number];
179183

180184
constructor(big: number, medium: number, small: number) {
181-
this.count = [big, medium, small];
185+
this.cnt = [0, big, medium, small];
182186
}
183187

184188
addCar(carType: number): boolean {
185-
if (this.count[carType - 1] === 0) {
189+
if (this.cnt[carType] === 0) {
186190
return false;
187191
}
188-
this.count[carType - 1]--;
192+
this.cnt[carType]--;
189193
return true;
190194
}
191195
}
@@ -201,26 +205,22 @@ class ParkingSystem {
201205

202206
```rust
203207
struct ParkingSystem {
204-
count: [i32; 3],
208+
cnt: [i32; 4]
205209
}
206210

207-
/**
208-
* `&self` means the method takes an immutable reference.
209-
* If you need a mutable reference, change it to `&mut self` instead.
210-
*/
211211
impl ParkingSystem {
212+
212213
fn new(big: i32, medium: i32, small: i32) -> Self {
213-
Self {
214-
count: [big, medium, small],
214+
ParkingSystem {
215+
cnt: [0, big, medium, small],
215216
}
216217
}
217218

218219
fn add_car(&mut self, car_type: i32) -> bool {
219-
let i = (car_type - 1) as usize;
220-
if self.count[i] == 0 {
220+
if self.cnt[car_type as usize] == 0 {
221221
return false;
222222
}
223-
self.count[i] -= 1;
223+
self.cnt[car_type as usize] -= 1;
224224
true
225225
}
226226
}

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

+19-15
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,15 @@ parkingSystem.addCar(1); // return false because there is no available slot for
6262

6363
<!-- solution:start -->
6464

65-
### Solution 1
65+
### Solution 1: Simulation
66+
67+
We use an array $\textit{cnt}$ of length 4 to represent the number of parking spaces for each type of car, where $\textit{cnt}[1]$, $\textit{cnt}[2]$, and $\textit{cnt}[3]$ represent the number of large, medium, and small parking spaces, respectively.
68+
69+
During initialization, we set $\textit{cnt}[1]$, $\textit{cnt}[2]$, and $\textit{cnt}[3]$ to the number of large, medium, and small parking spaces, respectively.
70+
71+
Each time a car parks, we check if there is a corresponding parking space in the parking lot. If not, we return $\textit{false}$; otherwise, we decrement the number of corresponding parking spaces by one and return $\textit{true}$.
72+
73+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
6674

6775
<!-- tabs:start -->
6876

@@ -169,17 +177,17 @@ func (this *ParkingSystem) AddCar(carType int) bool {
169177

170178
```ts
171179
class ParkingSystem {
172-
private count: [number, number, number];
180+
private cnt: [number, number, number, number];
173181

174182
constructor(big: number, medium: number, small: number) {
175-
this.count = [big, medium, small];
183+
this.cnt = [0, big, medium, small];
176184
}
177185

178186
addCar(carType: number): boolean {
179-
if (this.count[carType - 1] === 0) {
187+
if (this.cnt[carType] === 0) {
180188
return false;
181189
}
182-
this.count[carType - 1]--;
190+
this.cnt[carType]--;
183191
return true;
184192
}
185193
}
@@ -195,26 +203,22 @@ class ParkingSystem {
195203

196204
```rust
197205
struct ParkingSystem {
198-
count: [i32; 3],
206+
cnt: [i32; 4]
199207
}
200208

201-
/**
202-
* `&self` means the method takes an immutable reference.
203-
* If you need a mutable reference, change it to `&mut self` instead.
204-
*/
205209
impl ParkingSystem {
210+
206211
fn new(big: i32, medium: i32, small: i32) -> Self {
207-
Self {
208-
count: [big, medium, small],
212+
ParkingSystem {
213+
cnt: [0, big, medium, small],
209214
}
210215
}
211216

212217
fn add_car(&mut self, car_type: i32) -> bool {
213-
let i = (car_type - 1) as usize;
214-
if self.count[i] == 0 {
218+
if self.cnt[car_type as usize] == 0 {
215219
return false;
216220
}
217-
self.count[i] -= 1;
221+
self.cnt[car_type as usize] -= 1;
218222
true
219223
}
220224
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
struct ParkingSystem {
2-
count: [i32; 3],
2+
cnt: [i32; 4],
33
}
44

5-
/**
6-
* `&self` means the method takes an immutable reference.
7-
* If you need a mutable reference, change it to `&mut self` instead.
8-
*/
95
impl ParkingSystem {
106
fn new(big: i32, medium: i32, small: i32) -> Self {
11-
Self {
12-
count: [big, medium, small],
7+
ParkingSystem {
8+
cnt: [0, big, medium, small],
139
}
1410
}
1511

1612
fn add_car(&mut self, car_type: i32) -> bool {
17-
let i = (car_type - 1) as usize;
18-
if self.count[i] == 0 {
13+
if self.cnt[car_type as usize] == 0 {
1914
return false;
2015
}
21-
self.count[i] -= 1;
16+
self.cnt[car_type as usize] -= 1;
2217
true
2318
}
2419
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class ParkingSystem {
2-
private count: [number, number, number];
2+
private cnt: [number, number, number, number];
33

44
constructor(big: number, medium: number, small: number) {
5-
this.count = [big, medium, small];
5+
this.cnt = [0, big, medium, small];
66
}
77

88
addCar(carType: number): boolean {
9-
if (this.count[carType - 1] === 0) {
9+
if (this.cnt[carType] === 0) {
1010
return false;
1111
}
12-
this.count[carType - 1]--;
12+
this.cnt[carType]--;
1313
return true;
1414
}
1515
}

solution/1600-1699/1616.Split Two Strings to Make Palindrome/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,15 @@ Then, a<sub>prefix</sub> + b<sub>suffix</sub> = &quot;ula&quot; + &quot;alu&quot
7272

7373
<!-- solution:start -->
7474

75-
### Solution 1
75+
### Solution 1: Two Pointers
76+
77+
We can use two pointers, where one pointer $i$ starts from the beginning of string $a$, and the other pointer $j$ starts from the end of string $b$. If the characters pointed to by the two pointers are equal, then both pointers move towards the center until they encounter different characters or the two pointers cross.
78+
79+
If the two pointers cross, i.e., $i \geq j$, it means that $prefix$ and $suffix$ can already form a palindrome, and we return `true`. Otherwise, we need to check if $a[i,...j]$ or $b[i,...j]$ is a palindrome. If so, return `true`.
80+
81+
Otherwise, we try swapping the two strings $a$ and $b$ and repeat the same process.
82+
83+
The time complexity is $O(n)$, and the space complexity is $O(1)$. Where $n$ is the length of string $a$ or $b$.
7684

7785
<!-- tabs:start -->
7886

solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ tags:
7474

7575
### 方法一:哈希表 + DFS
7676

77+
我们用一个哈希表 $\textit{s}$ 记录数组 $\textit{nodes}$ 中所有节点的值,然后使用深度优先搜索,当遍历到的节点为空或者节点的值在哈希表 $\textit{s}$ 中时,返回当前节点。否则,递归遍历左右子树,如果左右子树的返回值都不为空,说明当前节点就是最近公共祖先,否则返回不为空的那个子树的返回值。
78+
79+
时间复杂度 $O(n + m)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是二叉树的节点数和数组 $\textit{nodes}$ 的长度。
80+
7781
<!-- tabs:start -->
7882

7983
#### Python3

solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ tags:
6666

6767
<!-- solution:start -->
6868

69-
### Solution 1
69+
### Solution 1: Hash Table + DFS
70+
71+
We use a hash table $\textit{s}$ to record the values of all nodes in the array $\textit{nodes}$, and then use depth-first search. When the node being traversed is null or its value is in the hash table $\textit{s}$, we return the current node. Otherwise, we recursively traverse the left and right subtrees. If the return values of both the left and right subtrees are not null, it means the current node is the lowest common ancestor. Otherwise, we return the non-null subtree's return value.
72+
73+
The time complexity is $O(n + m)$, and the space complexity is $O(n + m)$. Where $n$ and $m$ are the number of nodes in the binary tree and the length of the array $\textit{nodes}$, respectively.
7074

7175
<!-- tabs:start -->
7276

solution/1600-1699/1681.Minimum Incompatibility/README.md

-37
Original file line numberDiff line numberDiff line change
@@ -449,41 +449,4 @@ public class Solution {
449449

450450
<!-- solution:end -->
451451

452-
<!-- solution:start -->
453-
454-
### 方法二
455-
456-
<!-- tabs:start -->
457-
458-
#### Python3
459-
460-
```python
461-
class Solution:
462-
def minimumIncompatibility(self, nums: List[int], k: int) -> int:
463-
@cache
464-
def dfs(mask):
465-
if mask == (1 << n) - 1:
466-
return 0
467-
d = {v: i for i, v in enumerate(nums) if (mask >> i & 1) == 0}
468-
ans = inf
469-
if len(d) < m:
470-
return ans
471-
for vs in combinations(d.keys(), m):
472-
nxt = mask
473-
for v in vs:
474-
nxt |= 1 << d[v]
475-
ans = min(ans, max(vs) - min(vs) + dfs(nxt))
476-
return ans
477-
478-
n = len(nums)
479-
m = n // k
480-
ans = dfs(0)
481-
dfs.cache_clear()
482-
return ans if ans < inf else -1
483-
```
484-
485-
<!-- tabs:end -->
486-
487-
<!-- solution:end -->
488-
489452
<!-- problem:end -->

solution/1600-1699/1681.Minimum Incompatibility/README_EN.md

-37
Original file line numberDiff line numberDiff line change
@@ -447,41 +447,4 @@ public class Solution {
447447

448448
<!-- solution:end -->
449449

450-
<!-- solution:start -->
451-
452-
### Solution 2
453-
454-
<!-- tabs:start -->
455-
456-
#### Python3
457-
458-
```python
459-
class Solution:
460-
def minimumIncompatibility(self, nums: List[int], k: int) -> int:
461-
@cache
462-
def dfs(mask):
463-
if mask == (1 << n) - 1:
464-
return 0
465-
d = {v: i for i, v in enumerate(nums) if (mask >> i & 1) == 0}
466-
ans = inf
467-
if len(d) < m:
468-
return ans
469-
for vs in combinations(d.keys(), m):
470-
nxt = mask
471-
for v in vs:
472-
nxt |= 1 << d[v]
473-
ans = min(ans, max(vs) - min(vs) + dfs(nxt))
474-
return ans
475-
476-
n = len(nums)
477-
m = n // k
478-
ans = dfs(0)
479-
dfs.cache_clear()
480-
return ans if ans < inf else -1
481-
```
482-
483-
<!-- tabs:end -->
484-
485-
<!-- solution:end -->
486-
487450
<!-- problem:end -->

solution/1600-1699/1681.Minimum Incompatibility/Solution2.py

-22
This file was deleted.

0 commit comments

Comments
 (0)