Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update lc problems #4116

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions solution/1600-1699/1603.Design Parking System/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ parkingSystem.addCar(1); // 返回 false ,因为没有空的大车位,唯一

### 方法一:模拟

为每种车维护一个计数器,初始值为车位的数目。此后,每来一辆车,就将对应类型的计数器减 `1`。当计数器为 `0` 时,说明车位已满。
我们用一个长度为 $4$ 的数组 $\textit{cnt}$ 来表示停车场中每种车位的数量,其中 $\textit{cnt}[1]$, $\textit{cnt}[2]$, $\textit{cnt}[3]$ 分别表示大车位、中车位、小车位的数量。

在初始化时,我们将 $\textit{cnt}[1]$, $\textit{cnt}[2]$, $\textit{cnt}[3]$ 分别初始化为大车位、中车位、小车位的数量。

每次停车时,我们检查停车场中是否有对应车位,如果没有则返回 $\textit{false}$,否则将对应车位的数量减一,并返回 $\textit{true}$。

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

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

```ts
class ParkingSystem {
private count: [number, number, number];
private cnt: [number, number, number, number];

constructor(big: number, medium: number, small: number) {
this.count = [big, medium, small];
this.cnt = [0, big, medium, small];
}

addCar(carType: number): boolean {
if (this.count[carType - 1] === 0) {
if (this.cnt[carType] === 0) {
return false;
}
this.count[carType - 1]--;
this.cnt[carType]--;
return true;
}
}
Expand All @@ -201,26 +205,22 @@ class ParkingSystem {

```rust
struct ParkingSystem {
count: [i32; 3],
cnt: [i32; 4]
}

/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl ParkingSystem {

fn new(big: i32, medium: i32, small: i32) -> Self {
Self {
count: [big, medium, small],
ParkingSystem {
cnt: [0, big, medium, small],
}
}

fn add_car(&mut self, car_type: i32) -> bool {
let i = (car_type - 1) as usize;
if self.count[i] == 0 {
if self.cnt[car_type as usize] == 0 {
return false;
}
self.count[i] -= 1;
self.cnt[car_type as usize] -= 1;
true
}
}
Expand Down
34 changes: 19 additions & 15 deletions solution/1600-1699/1603.Design Parking System/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ parkingSystem.addCar(1); // return false because there is no available slot for

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

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.

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.

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}$.

The time complexity is $O(1)$, and the space complexity is $O(1)$.

<!-- tabs:start -->

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

```ts
class ParkingSystem {
private count: [number, number, number];
private cnt: [number, number, number, number];

constructor(big: number, medium: number, small: number) {
this.count = [big, medium, small];
this.cnt = [0, big, medium, small];
}

addCar(carType: number): boolean {
if (this.count[carType - 1] === 0) {
if (this.cnt[carType] === 0) {
return false;
}
this.count[carType - 1]--;
this.cnt[carType]--;
return true;
}
}
Expand All @@ -195,26 +203,22 @@ class ParkingSystem {

```rust
struct ParkingSystem {
count: [i32; 3],
cnt: [i32; 4]
}

/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl ParkingSystem {

fn new(big: i32, medium: i32, small: i32) -> Self {
Self {
count: [big, medium, small],
ParkingSystem {
cnt: [0, big, medium, small],
}
}

fn add_car(&mut self, car_type: i32) -> bool {
let i = (car_type - 1) as usize;
if self.count[i] == 0 {
if self.cnt[car_type as usize] == 0 {
return false;
}
self.count[i] -= 1;
self.cnt[car_type as usize] -= 1;
true
}
}
Expand Down
15 changes: 5 additions & 10 deletions solution/1600-1699/1603.Design Parking System/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
struct ParkingSystem {
count: [i32; 3],
cnt: [i32; 4],
}

/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl ParkingSystem {
fn new(big: i32, medium: i32, small: i32) -> Self {
Self {
count: [big, medium, small],
ParkingSystem {
cnt: [0, big, medium, small],
}
}

fn add_car(&mut self, car_type: i32) -> bool {
let i = (car_type - 1) as usize;
if self.count[i] == 0 {
if self.cnt[car_type as usize] == 0 {
return false;
}
self.count[i] -= 1;
self.cnt[car_type as usize] -= 1;
true
}
}
8 changes: 4 additions & 4 deletions solution/1600-1699/1603.Design Parking System/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
class ParkingSystem {
private count: [number, number, number];
private cnt: [number, number, number, number];

constructor(big: number, medium: number, small: number) {
this.count = [big, medium, small];
this.cnt = [0, big, medium, small];
}

addCar(carType: number): boolean {
if (this.count[carType - 1] === 0) {
if (this.cnt[carType] === 0) {
return false;
}
this.count[carType - 1]--;
this.cnt[carType]--;
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,15 @@ Then, a<sub>prefix</sub> + b<sub>suffix</sub> = &quot;ula&quot; + &quot;alu&quot

<!-- solution:start -->

### Solution 1
### Solution 1: Two Pointers

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.

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`.

Otherwise, we try swapping the two strings $a$ and $b$ and repeat the same process.

The time complexity is $O(n)$, and the space complexity is $O(1)$. Where $n$ is the length of string $a$ or $b$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ tags:

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

我们用一个哈希表 $\textit{s}$ 记录数组 $\textit{nodes}$ 中所有节点的值,然后使用深度优先搜索,当遍历到的节点为空或者节点的值在哈希表 $\textit{s}$ 中时,返回当前节点。否则,递归遍历左右子树,如果左右子树的返回值都不为空,说明当前节点就是最近公共祖先,否则返回不为空的那个子树的返回值。

时间复杂度 $O(n + m)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是二叉树的节点数和数组 $\textit{nodes}$ 的长度。

<!-- tabs:start -->

#### Python3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Hash Table + DFS

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.

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.

<!-- tabs:start -->

Expand Down
37 changes: 0 additions & 37 deletions solution/1600-1699/1681.Minimum Incompatibility/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,41 +449,4 @@ public class Solution {

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Python3

```python
class Solution:
def minimumIncompatibility(self, nums: List[int], k: int) -> int:
@cache
def dfs(mask):
if mask == (1 << n) - 1:
return 0
d = {v: i for i, v in enumerate(nums) if (mask >> i & 1) == 0}
ans = inf
if len(d) < m:
return ans
for vs in combinations(d.keys(), m):
nxt = mask
for v in vs:
nxt |= 1 << d[v]
ans = min(ans, max(vs) - min(vs) + dfs(nxt))
return ans

n = len(nums)
m = n // k
ans = dfs(0)
dfs.cache_clear()
return ans if ans < inf else -1
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
37 changes: 0 additions & 37 deletions solution/1600-1699/1681.Minimum Incompatibility/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,41 +447,4 @@ public class Solution {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Python3

```python
class Solution:
def minimumIncompatibility(self, nums: List[int], k: int) -> int:
@cache
def dfs(mask):
if mask == (1 << n) - 1:
return 0
d = {v: i for i, v in enumerate(nums) if (mask >> i & 1) == 0}
ans = inf
if len(d) < m:
return ans
for vs in combinations(d.keys(), m):
nxt = mask
for v in vs:
nxt |= 1 << d[v]
ans = min(ans, max(vs) - min(vs) + dfs(nxt))
return ans

n = len(nums)
m = n // k
ans = dfs(0)
dfs.cache_clear()
return ans if ans < inf else -1
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
22 changes: 0 additions & 22 deletions solution/1600-1699/1681.Minimum Incompatibility/Solution2.py

This file was deleted.