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 #2737

Merged
merged 1 commit into from
May 7, 2024
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
5 changes: 3 additions & 2 deletions lcci/16.04.Tic-Tac-Toe/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ function tictactoe(board: string[]): string {
return hasEmptyGrid ? 'Pending' : 'Draw';
}
```

```swift
class Solution {
func tictactoe(_ board: [String]) -> String {
Expand All @@ -238,7 +239,7 @@ class Solution {
var cols = Array(repeating: 0, count: n)
var diagonal = 0, antiDiagonal = 0
var hasEmptyGrid = false

for i in 0..<n {
for j in 0..<n {
let c = Array(board[i])[j]
Expand All @@ -260,7 +261,7 @@ class Solution {
}
}
}

return hasEmptyGrid ? "Pending" : "Draw"
}
}
Expand Down
4 changes: 2 additions & 2 deletions lcci/16.04.Tic-Tac-Toe/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class Solution {
var cols = Array(repeating: 0, count: n)
var diagonal = 0, antiDiagonal = 0
var hasEmptyGrid = false

for i in 0..<n {
for j in 0..<n {
let c = Array(board[i])[j]
Expand All @@ -273,7 +273,7 @@ class Solution {
}
}
}

return hasEmptyGrid ? "Pending" : "Draw"
}
}
Expand Down
6 changes: 3 additions & 3 deletions lcci/16.06.Smallest Difference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class Solution {
func smallestDifference(_ a: [Int], _ b: [Int]) -> Int {
let sortedB = b.sorted()
var ans = Int.max

for x in a {
let j = search(sortedB, x)
if j < sortedB.count {
Expand All @@ -156,10 +156,10 @@ class Solution {
ans = min(ans, abs(x - sortedB[j - 1]))
}
}

return ans
}

private func search(_ nums: [Int], _ x: Int) -> Int {
var l = 0
var r = nums.count
Expand Down
6 changes: 3 additions & 3 deletions lcci/16.06.Smallest Difference/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class Solution {
func smallestDifference(_ a: [Int], _ b: [Int]) -> Int {
let sortedB = b.sorted()
var ans = Int.max

for x in a {
let j = search(sortedB, x)
if j < sortedB.count {
Expand All @@ -162,10 +162,10 @@ class Solution {
ans = min(ans, abs(x - sortedB[j - 1]))
}
}

return ans
}

private func search(_ nums: [Int], _ x: Int) -> Int {
var l = 0
var r = nums.count
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li>
<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>
<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>
<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>
<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>
</ol>

<p>Return the integer as the final result.</p>
Expand Down
2 changes: 1 addition & 1 deletion solution/0300-0399/0305.Number of Islands II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## 题目描述

Expand Down
2 changes: 1 addition & 1 deletion solution/0300-0399/0305.Number of Islands II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## Description

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## 题目描述

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## Description

Expand Down
2 changes: 1 addition & 1 deletion solution/0700-0799/0745.Prefix and Suffix Search/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## 题目描述

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## Description

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## 题目描述

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## Description

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## 题目描述

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## Description

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## 题目描述

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## Description

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## 题目描述

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## Description

Expand Down
50 changes: 16 additions & 34 deletions solution/1400-1499/1463.Cherry Pickup II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ $$

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

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

<!-- tabs:start -->

```python
Expand Down Expand Up @@ -180,7 +178,7 @@ public:
```

```go
func cherryPickup(grid [][]int) int {
func cherryPickup(grid [][]int) (ans int) {
m, n := len(grid), len(grid[0])
f := make([][][]int, m)
for i := range f {
Expand Down Expand Up @@ -210,23 +208,20 @@ func cherryPickup(grid [][]int) int {
}
}
}
ans := 0
for j1 := 0; j1 < n; j1++ {
for j2 := 0; j2 < n; j2++ {
ans = max(ans, f[m-1][j1][j2])
}
ans = max(ans, slices.Max(f[m-1][j1]))
}
return ans
return
}
```

```ts
function cherryPickup(grid: number[][]): number {
const m = grid.length;
const n = grid[0].length;
const f: number[][][] = new Array(m)
.fill(0)
.map(() => new Array(n).fill(0).map(() => new Array(n).fill(-1)));
const f = Array.from({ length: m }, () =>
Array.from({ length: n }, () => Array.from({ length: n }, () => -1)),
);
f[0][0][n - 1] = grid[0][0] + grid[0][n - 1];
for (let i = 1; i < m; ++i) {
for (let j1 = 0; j1 < n; ++j1) {
Expand All @@ -242,19 +237,15 @@ function cherryPickup(grid: number[][]): number {
}
}
}
let ans = 0;
for (let j1 = 0; j1 < n; ++j1) {
for (let j2 = 0; j2 < n; ++j2) {
ans = Math.max(ans, f[m - 1][j1][j2]);
}
}
return ans;
return Math.max(...f[m - 1].flat());
}
```

<!-- tabs:end -->

### 方法二
### 方法二:动态规划(空间优化)

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

<!-- tabs:start -->

Expand Down Expand Up @@ -351,7 +342,7 @@ public:
```

```go
func cherryPickup(grid [][]int) int {
func cherryPickup(grid [][]int) (ans int) {
m, n := len(grid), len(grid[0])
f := make([][]int, n)
g := make([][]int, n)
Expand Down Expand Up @@ -382,22 +373,19 @@ func cherryPickup(grid [][]int) int {
}
f, g = g, f
}
ans := 0
for j1 := 0; j1 < n; j1++ {
for j2 := 0; j2 < n; j2++ {
ans = max(ans, f[j1][j2])
}
ans = max(ans, slices.Max(f[j1]))
}
return ans
return
}
```

```ts
function cherryPickup(grid: number[][]): number {
const m = grid.length;
const n = grid[0].length;
let f: number[][] = new Array(n).fill(0).map(() => new Array(n).fill(-1));
let g: number[][] = new Array(n).fill(0).map(() => new Array(n).fill(-1));
let f: number[][] = Array.from({ length: n }, () => Array.from({ length: n }, () => -1));
let g: number[][] = Array.from({ length: n }, () => Array.from({ length: n }, () => -1));
f[0][n - 1] = grid[0][0] + grid[0][n - 1];
for (let i = 1; i < m; ++i) {
for (let j1 = 0; j1 < n; ++j1) {
Expand All @@ -414,13 +402,7 @@ function cherryPickup(grid: number[][]): number {
}
[f, g] = [g, f];
}
let ans = 0;
for (let j1 = 0; j1 < n; ++j1) {
for (let j2 = 0; j2 < n; ++j2) {
ans = Math.max(ans, f[j1][j2]);
}
}
return ans;
return Math.max(...f.flat());
}
```

Expand Down
Loading
Loading