diff --git a/solution/1600-1699/1603.Design Parking System/README.md b/solution/1600-1699/1603.Design Parking System/README.md index 6b57f48a95b37..15964ed5062f7 100644 --- a/solution/1600-1699/1603.Design Parking System/README.md +++ b/solution/1600-1699/1603.Design Parking System/README.md @@ -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)$。 @@ -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; } } @@ -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 } } diff --git a/solution/1600-1699/1603.Design Parking System/README_EN.md b/solution/1600-1699/1603.Design Parking System/README_EN.md index db3c957bd405d..947dde26591e6 100644 --- a/solution/1600-1699/1603.Design Parking System/README_EN.md +++ b/solution/1600-1699/1603.Design Parking System/README_EN.md @@ -62,7 +62,15 @@ parkingSystem.addCar(1); // return false because there is no available slot for -### 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)$. @@ -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; } } @@ -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 } } diff --git a/solution/1600-1699/1603.Design Parking System/Solution.rs b/solution/1600-1699/1603.Design Parking System/Solution.rs index bb55e867617bf..ea4e2dfbe8913 100644 --- a/solution/1600-1699/1603.Design Parking System/Solution.rs +++ b/solution/1600-1699/1603.Design Parking System/Solution.rs @@ -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 } } diff --git a/solution/1600-1699/1603.Design Parking System/Solution.ts b/solution/1600-1699/1603.Design Parking System/Solution.ts index bddc2905bf252..06e9243b9ddb7 100644 --- a/solution/1600-1699/1603.Design Parking System/Solution.ts +++ b/solution/1600-1699/1603.Design Parking System/Solution.ts @@ -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; } } diff --git a/solution/1600-1699/1616.Split Two Strings to Make Palindrome/README_EN.md b/solution/1600-1699/1616.Split Two Strings to Make Palindrome/README_EN.md index d62a87129ab99..8846a73797682 100644 --- a/solution/1600-1699/1616.Split Two Strings to Make Palindrome/README_EN.md +++ b/solution/1600-1699/1616.Split Two Strings to Make Palindrome/README_EN.md @@ -72,7 +72,15 @@ Then, aprefix + bsuffix = "ula" + "alu" -### 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$. diff --git a/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README.md b/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README.md index 3ddca17109029..c66ae1ad01219 100644 --- a/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README.md +++ b/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README.md @@ -74,6 +74,10 @@ tags: ### 方法一:哈希表 + DFS +我们用一个哈希表 $\textit{s}$ 记录数组 $\textit{nodes}$ 中所有节点的值,然后使用深度优先搜索,当遍历到的节点为空或者节点的值在哈希表 $\textit{s}$ 中时,返回当前节点。否则,递归遍历左右子树,如果左右子树的返回值都不为空,说明当前节点就是最近公共祖先,否则返回不为空的那个子树的返回值。 + +时间复杂度 $O(n + m)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是二叉树的节点数和数组 $\textit{nodes}$ 的长度。 + #### Python3 diff --git a/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README_EN.md b/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README_EN.md index d735eb161fd8b..7383c0d963781 100644 --- a/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README_EN.md +++ b/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README_EN.md @@ -66,7 +66,11 @@ tags: -### 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. diff --git a/solution/1600-1699/1681.Minimum Incompatibility/README.md b/solution/1600-1699/1681.Minimum Incompatibility/README.md index e996de164bd73..680133ad02e25 100644 --- a/solution/1600-1699/1681.Minimum Incompatibility/README.md +++ b/solution/1600-1699/1681.Minimum Incompatibility/README.md @@ -449,41 +449,4 @@ public class Solution { - - -### 方法二 - - - -#### 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 -``` - - - - - diff --git a/solution/1600-1699/1681.Minimum Incompatibility/README_EN.md b/solution/1600-1699/1681.Minimum Incompatibility/README_EN.md index be663193531e4..70d6e1acbabcd 100644 --- a/solution/1600-1699/1681.Minimum Incompatibility/README_EN.md +++ b/solution/1600-1699/1681.Minimum Incompatibility/README_EN.md @@ -447,41 +447,4 @@ public class Solution { - - -### Solution 2 - - - -#### 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 -``` - - - - - diff --git a/solution/1600-1699/1681.Minimum Incompatibility/Solution2.py b/solution/1600-1699/1681.Minimum Incompatibility/Solution2.py deleted file mode 100644 index 40fe763df8dd3..0000000000000 --- a/solution/1600-1699/1681.Minimum Incompatibility/Solution2.py +++ /dev/null @@ -1,22 +0,0 @@ -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