Skip to content

Commit 2ea76d7

Browse files
authored
feat: update lc problems (doocs#2737)
1 parent 4507b47 commit 2ea76d7

File tree

49 files changed

+429
-214
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+429
-214
lines changed

lcci/16.04.Tic-Tac-Toe/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ function tictactoe(board: string[]): string {
230230
return hasEmptyGrid ? 'Pending' : 'Draw';
231231
}
232232
```
233+
233234
```swift
234235
class Solution {
235236
func tictactoe(_ board: [String]) -> String {
@@ -238,7 +239,7 @@ class Solution {
238239
var cols = Array(repeating: 0, count: n)
239240
var diagonal = 0, antiDiagonal = 0
240241
var hasEmptyGrid = false
241-
242+
242243
for i in 0..<n {
243244
for j in 0..<n {
244245
let c = Array(board[i])[j]
@@ -260,7 +261,7 @@ class Solution {
260261
}
261262
}
262263
}
263-
264+
264265
return hasEmptyGrid ? "Pending" : "Draw"
265266
}
266267
}

lcci/16.04.Tic-Tac-Toe/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ class Solution {
251251
var cols = Array(repeating: 0, count: n)
252252
var diagonal = 0, antiDiagonal = 0
253253
var hasEmptyGrid = false
254-
254+
255255
for i in 0..<n {
256256
for j in 0..<n {
257257
let c = Array(board[i])[j]
@@ -273,7 +273,7 @@ class Solution {
273273
}
274274
}
275275
}
276-
276+
277277
return hasEmptyGrid ? "Pending" : "Draw"
278278
}
279279
}

lcci/16.06.Smallest Difference/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class Solution {
146146
func smallestDifference(_ a: [Int], _ b: [Int]) -> Int {
147147
let sortedB = b.sorted()
148148
var ans = Int.max
149-
149+
150150
for x in a {
151151
let j = search(sortedB, x)
152152
if j < sortedB.count {
@@ -156,10 +156,10 @@ class Solution {
156156
ans = min(ans, abs(x - sortedB[j - 1]))
157157
}
158158
}
159-
159+
160160
return ans
161161
}
162-
162+
163163
private func search(_ nums: [Int], _ x: Int) -> Int {
164164
var l = 0
165165
var r = nums.count

lcci/16.06.Smallest Difference/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class Solution {
152152
func smallestDifference(_ a: [Int], _ b: [Int]) -> Int {
153153
let sortedB = b.sorted()
154154
var ans = Int.max
155-
155+
156156
for x in a {
157157
let j = search(sortedB, x)
158158
if j < sortedB.count {
@@ -162,10 +162,10 @@ class Solution {
162162
ans = min(ans, abs(x - sortedB[j - 1]))
163163
}
164164
}
165-
165+
166166
return ans
167167
}
168-
168+
169169
private func search(_ nums: [Int], _ x: Int) -> Int {
170170
var l = 0
171171
var r = nums.count

solution/0000-0099/0008.String to Integer (atoi)/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li>
1515
<li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li>
1616
<li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li>
17-
<li><strong>Edge case</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li>
17+
<li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li>
1818
</ol>
1919

2020
<p>Return the integer as the final result.</p>

solution/0300-0399/0305.Number of Islands II/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/0300-0399/0305.Number%20of%20Islands%20II/README_EN.md)
44

5-
<!-- tags:并查集,数组 -->
5+
<!-- tags:并查集,数组,哈希表 -->
66

77
## 题目描述
88

solution/0300-0399/0305.Number of Islands II/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/0300-0399/0305.Number%20of%20Islands%20II/README.md)
44

5-
<!-- tags:Union Find,Array -->
5+
<!-- tags:Union Find,Array,Hash Table -->
66

77
## Description
88

solution/0300-0399/0314.Binary Tree Vertical Order Traversal/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/0300-0399/0314.Binary%20Tree%20Vertical%20Order%20Traversal/README_EN.md)
44

5-
<!-- tags:树,深度优先搜索,广度优先搜索,哈希表,二叉树 -->
5+
<!-- tags:树,深度优先搜索,广度优先搜索,哈希表,二叉树,排序 -->
66

77
## 题目描述
88

solution/0300-0399/0314.Binary Tree Vertical Order Traversal/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/0300-0399/0314.Binary%20Tree%20Vertical%20Order%20Traversal/README.md)
44

5-
<!-- tags:Tree,Depth-First Search,Breadth-First Search,Hash Table,Binary Tree -->
5+
<!-- tags:Tree,Depth-First Search,Breadth-First Search,Hash Table,Binary Tree,Sorting -->
66

77
## Description
88

solution/0700-0799/0745.Prefix and Suffix Search/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/0700-0799/0745.Prefix%20and%20Suffix%20Search/README_EN.md)
44

5-
<!-- tags:设计,字典树,哈希表,字符串 -->
5+
<!-- tags:设计,字典树,数组,哈希表,字符串 -->
66

77
## 题目描述
88

solution/0700-0799/0745.Prefix and Suffix Search/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/0700-0799/0745.Prefix%20and%20Suffix%20Search/README.md)
44

5-
<!-- tags:Design,Trie,Hash Table,String -->
5+
<!-- tags:Design,Trie,Array,Hash Table,String -->
66

77
## Description
88

solution/0800-0899/0863.All Nodes Distance K in Binary Tree/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/0800-0899/0863.All%20Nodes%20Distance%20K%20in%20Binary%20Tree/README_EN.md)
44

5-
<!-- tags:树,深度优先搜索,广度优先搜索,二叉树 -->
5+
<!-- tags:树,深度优先搜索,广度优先搜索,哈希表,二叉树 -->
66

77
## 题目描述
88

solution/0800-0899/0863.All Nodes Distance K in Binary Tree/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/0800-0899/0863.All%20Nodes%20Distance%20K%20in%20Binary%20Tree/README.md)
44

5-
<!-- tags:Tree,Depth-First Search,Breadth-First Search,Binary Tree -->
5+
<!-- tags:Tree,Depth-First Search,Breadth-First Search,Hash Table,Binary Tree -->
66

77
## Description
88

solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/0900-0999/0987.Vertical%20Order%20Traversal%20of%20a%20Binary%20Tree/README_EN.md)
44

5-
<!-- tags:树,深度优先搜索,广度优先搜索,哈希表,二叉树 -->
5+
<!-- tags:树,深度优先搜索,广度优先搜索,哈希表,二叉树,排序 -->
66

77
## 题目描述
88

solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/0900-0999/0987.Vertical%20Order%20Traversal%20of%20a%20Binary%20Tree/README.md)
44

5-
<!-- tags:Tree,Depth-First Search,Breadth-First Search,Hash Table,Binary Tree -->
5+
<!-- tags:Tree,Depth-First Search,Breadth-First Search,Hash Table,Binary Tree,Sorting -->
66

77
## Description
88

solution/1000-1099/1058.Minimize Rounding Error to Meet Target/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/1000-1099/1058.Minimize%20Rounding%20Error%20to%20Meet%20Target/README_EN.md)
44

5-
<!-- tags:贪心,数组,数学,字符串 -->
5+
<!-- tags:贪心,数组,数学,字符串,排序 -->
66

77
## 题目描述
88

solution/1000-1099/1058.Minimize Rounding Error to Meet Target/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/1000-1099/1058.Minimize%20Rounding%20Error%20to%20Meet%20Target/README.md)
44

5-
<!-- tags:Greedy,Array,Math,String -->
5+
<!-- tags:Greedy,Array,Math,String,Sorting -->
66

77
## Description
88

solution/1000-1099/1059.All Paths from Source Lead to Destination/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[English Version](/solution/1000-1099/1059.All%20Paths%20from%20Source%20Lead%20to%20Destination/README_EN.md)
44

5-
<!-- tags:深度优先搜索,图 -->
5+
<!-- tags:图,拓扑排序 -->
66

77
## 题目描述
88

solution/1000-1099/1059.All Paths from Source Lead to Destination/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[中文文档](/solution/1000-1099/1059.All%20Paths%20from%20Source%20Lead%20to%20Destination/README.md)
44

5-
<!-- tags:Depth-First Search,Graph -->
5+
<!-- tags:Graph,Topological Sort -->
66

77
## Description
88

solution/1400-1499/1463.Cherry Pickup II/README.md

+16-34
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ $$
8989

9090
时间复杂度 $O(m \times n^2)$,空间复杂度 $O(m \times n^2)$。其中 $m$ 和 $n$ 分别是网格的行数和列数。
9191

92-
注意到 $f[i][j_1][j_2]$ 的计算只和 $f[i-1][y_1][y_2]$ 有关,因此我们可以使用滚动数组优化空间复杂度,空间复杂度优化后的时间复杂度为 $O(n^2)$。
93-
9492
<!-- tabs:start -->
9593

9694
```python
@@ -180,7 +178,7 @@ public:
180178
```
181179
182180
```go
183-
func cherryPickup(grid [][]int) int {
181+
func cherryPickup(grid [][]int) (ans int) {
184182
m, n := len(grid), len(grid[0])
185183
f := make([][][]int, m)
186184
for i := range f {
@@ -210,23 +208,20 @@ func cherryPickup(grid [][]int) int {
210208
}
211209
}
212210
}
213-
ans := 0
214211
for j1 := 0; j1 < n; j1++ {
215-
for j2 := 0; j2 < n; j2++ {
216-
ans = max(ans, f[m-1][j1][j2])
217-
}
212+
ans = max(ans, slices.Max(f[m-1][j1]))
218213
}
219-
return ans
214+
return
220215
}
221216
```
222217

223218
```ts
224219
function cherryPickup(grid: number[][]): number {
225220
const m = grid.length;
226221
const n = grid[0].length;
227-
const f: number[][][] = new Array(m)
228-
.fill(0)
229-
.map(() => new Array(n).fill(0).map(() => new Array(n).fill(-1)));
222+
const f = Array.from({ length: m }, () =>
223+
Array.from({ length: n }, () => Array.from({ length: n }, () => -1)),
224+
);
230225
f[0][0][n - 1] = grid[0][0] + grid[0][n - 1];
231226
for (let i = 1; i < m; ++i) {
232227
for (let j1 = 0; j1 < n; ++j1) {
@@ -242,19 +237,15 @@ function cherryPickup(grid: number[][]): number {
242237
}
243238
}
244239
}
245-
let ans = 0;
246-
for (let j1 = 0; j1 < n; ++j1) {
247-
for (let j2 = 0; j2 < n; ++j2) {
248-
ans = Math.max(ans, f[m - 1][j1][j2]);
249-
}
250-
}
251-
return ans;
240+
return Math.max(...f[m - 1].flat());
252241
}
253242
```
254243

255244
<!-- tabs:end -->
256245

257-
### 方法二
246+
### 方法二:动态规划(空间优化)
247+
248+
注意到 $f[i][j_1][j_2]$ 的计算只和 $f[i-1][y_1][y_2]$ 有关,因此我们可以使用滚动数组优化空间复杂度,空间复杂度优化后的时间复杂度为 $O(n^2)$。
258249

259250
<!-- tabs:start -->
260251

@@ -351,7 +342,7 @@ public:
351342
```
352343
353344
```go
354-
func cherryPickup(grid [][]int) int {
345+
func cherryPickup(grid [][]int) (ans int) {
355346
m, n := len(grid), len(grid[0])
356347
f := make([][]int, n)
357348
g := make([][]int, n)
@@ -382,22 +373,19 @@ func cherryPickup(grid [][]int) int {
382373
}
383374
f, g = g, f
384375
}
385-
ans := 0
386376
for j1 := 0; j1 < n; j1++ {
387-
for j2 := 0; j2 < n; j2++ {
388-
ans = max(ans, f[j1][j2])
389-
}
377+
ans = max(ans, slices.Max(f[j1]))
390378
}
391-
return ans
379+
return
392380
}
393381
```
394382

395383
```ts
396384
function cherryPickup(grid: number[][]): number {
397385
const m = grid.length;
398386
const n = grid[0].length;
399-
let f: number[][] = new Array(n).fill(0).map(() => new Array(n).fill(-1));
400-
let g: number[][] = new Array(n).fill(0).map(() => new Array(n).fill(-1));
387+
let f: number[][] = Array.from({ length: n }, () => Array.from({ length: n }, () => -1));
388+
let g: number[][] = Array.from({ length: n }, () => Array.from({ length: n }, () => -1));
401389
f[0][n - 1] = grid[0][0] + grid[0][n - 1];
402390
for (let i = 1; i < m; ++i) {
403391
for (let j1 = 0; j1 < n; ++j1) {
@@ -414,13 +402,7 @@ function cherryPickup(grid: number[][]): number {
414402
}
415403
[f, g] = [g, f];
416404
}
417-
let ans = 0;
418-
for (let j1 = 0; j1 < n; ++j1) {
419-
for (let j2 = 0; j2 < n; ++j2) {
420-
ans = Math.max(ans, f[j1][j2]);
421-
}
422-
}
423-
return ans;
405+
return Math.max(...f.flat());
424406
}
425407
```
426408

0 commit comments

Comments
 (0)