diff --git a/lcci/16.04.Tic-Tac-Toe/README.md b/lcci/16.04.Tic-Tac-Toe/README.md index 7e4dfa99463b4..800942303edc8 100644 --- a/lcci/16.04.Tic-Tac-Toe/README.md +++ b/lcci/16.04.Tic-Tac-Toe/README.md @@ -230,6 +230,7 @@ function tictactoe(board: string[]): string { return hasEmptyGrid ? 'Pending' : 'Draw'; } ``` + ```swift class Solution { func tictactoe(_ board: [String]) -> String { @@ -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.. Int { let sortedB = b.sorted() var ans = Int.max - + for x in a { let j = search(sortedB, x) if j < sortedB.count { @@ -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 diff --git a/lcci/16.06.Smallest Difference/README_EN.md b/lcci/16.06.Smallest Difference/README_EN.md index 62b02e0082310..e024f8f075f74 100644 --- a/lcci/16.06.Smallest Difference/README_EN.md +++ b/lcci/16.06.Smallest Difference/README_EN.md @@ -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 { @@ -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 diff --git a/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md b/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md index ad67eb6503ca9..dfb954086acf7 100644 --- a/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md +++ b/solution/0000-0099/0008.String to Integer (atoi)/README_EN.md @@ -14,7 +14,7 @@
  • Whitespace: Ignore any leading whitespace (" ").
  • Signedness: Determine the sign by checking if the next character is '-' or '+', assuming positivity is neither present.
  • Conversion: Read the integer by skipping leading zeros 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.
  • -
  • Edge case: If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then round the integer to remain in the range. Specifically, integers less than -231 should be rounded to -231, and integers greater than 231 - 1 should be rounded to 231 - 1.
  • +
  • Rounding: If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then round the integer to remain in the range. Specifically, integers less than -231 should be rounded to -231, and integers greater than 231 - 1 should be rounded to 231 - 1.
  • Return the integer as the final result.

    diff --git a/solution/0300-0399/0305.Number of Islands II/README.md b/solution/0300-0399/0305.Number of Islands II/README.md index 9ceb83f08de5b..225c4909c6c60 100644 --- a/solution/0300-0399/0305.Number of Islands II/README.md +++ b/solution/0300-0399/0305.Number of Islands II/README.md @@ -2,7 +2,7 @@ [English Version](/solution/0300-0399/0305.Number%20of%20Islands%20II/README_EN.md) - + ## 题目描述 diff --git a/solution/0300-0399/0305.Number of Islands II/README_EN.md b/solution/0300-0399/0305.Number of Islands II/README_EN.md index 10f0a94b75f1b..ffc8aa71c575a 100644 --- a/solution/0300-0399/0305.Number of Islands II/README_EN.md +++ b/solution/0300-0399/0305.Number of Islands II/README_EN.md @@ -2,7 +2,7 @@ [中文文档](/solution/0300-0399/0305.Number%20of%20Islands%20II/README.md) - + ## Description diff --git a/solution/0300-0399/0314.Binary Tree Vertical Order Traversal/README.md b/solution/0300-0399/0314.Binary Tree Vertical Order Traversal/README.md index 4cdc823635007..ec840acf63afa 100644 --- a/solution/0300-0399/0314.Binary Tree Vertical Order Traversal/README.md +++ b/solution/0300-0399/0314.Binary Tree Vertical Order Traversal/README.md @@ -2,7 +2,7 @@ [English Version](/solution/0300-0399/0314.Binary%20Tree%20Vertical%20Order%20Traversal/README_EN.md) - + ## 题目描述 diff --git a/solution/0300-0399/0314.Binary Tree Vertical Order Traversal/README_EN.md b/solution/0300-0399/0314.Binary Tree Vertical Order Traversal/README_EN.md index 5e9b7dd846dd7..06b9860b97bfd 100644 --- a/solution/0300-0399/0314.Binary Tree Vertical Order Traversal/README_EN.md +++ b/solution/0300-0399/0314.Binary Tree Vertical Order Traversal/README_EN.md @@ -2,7 +2,7 @@ [中文文档](/solution/0300-0399/0314.Binary%20Tree%20Vertical%20Order%20Traversal/README.md) - + ## Description diff --git a/solution/0700-0799/0745.Prefix and Suffix Search/README.md b/solution/0700-0799/0745.Prefix and Suffix Search/README.md index bf9ecce310c51..5db6598eaf52e 100644 --- a/solution/0700-0799/0745.Prefix and Suffix Search/README.md +++ b/solution/0700-0799/0745.Prefix and Suffix Search/README.md @@ -2,7 +2,7 @@ [English Version](/solution/0700-0799/0745.Prefix%20and%20Suffix%20Search/README_EN.md) - + ## 题目描述 diff --git a/solution/0700-0799/0745.Prefix and Suffix Search/README_EN.md b/solution/0700-0799/0745.Prefix and Suffix Search/README_EN.md index ffbb825b30a7e..1ec0759b66dfb 100644 --- a/solution/0700-0799/0745.Prefix and Suffix Search/README_EN.md +++ b/solution/0700-0799/0745.Prefix and Suffix Search/README_EN.md @@ -2,7 +2,7 @@ [中文文档](/solution/0700-0799/0745.Prefix%20and%20Suffix%20Search/README.md) - + ## Description diff --git a/solution/0800-0899/0863.All Nodes Distance K in Binary Tree/README.md b/solution/0800-0899/0863.All Nodes Distance K in Binary Tree/README.md index 799386d3d29d8..67e870fe74500 100644 --- a/solution/0800-0899/0863.All Nodes Distance K in Binary Tree/README.md +++ b/solution/0800-0899/0863.All Nodes Distance K in Binary Tree/README.md @@ -2,7 +2,7 @@ [English Version](/solution/0800-0899/0863.All%20Nodes%20Distance%20K%20in%20Binary%20Tree/README_EN.md) - + ## 题目描述 diff --git a/solution/0800-0899/0863.All Nodes Distance K in Binary Tree/README_EN.md b/solution/0800-0899/0863.All Nodes Distance K in Binary Tree/README_EN.md index d8660ec0526f2..8b3233e9eaac9 100644 --- a/solution/0800-0899/0863.All Nodes Distance K in Binary Tree/README_EN.md +++ b/solution/0800-0899/0863.All Nodes Distance K in Binary Tree/README_EN.md @@ -2,7 +2,7 @@ [中文文档](/solution/0800-0899/0863.All%20Nodes%20Distance%20K%20in%20Binary%20Tree/README.md) - + ## Description diff --git a/solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README.md b/solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README.md index c0811c0b7a211..3199f429d692f 100644 --- a/solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README.md +++ b/solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README.md @@ -2,7 +2,7 @@ [English Version](/solution/0900-0999/0987.Vertical%20Order%20Traversal%20of%20a%20Binary%20Tree/README_EN.md) - + ## 题目描述 diff --git a/solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README_EN.md b/solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README_EN.md index 730949c300319..888bdbfbaafbb 100644 --- a/solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README_EN.md +++ b/solution/0900-0999/0987.Vertical Order Traversal of a Binary Tree/README_EN.md @@ -2,7 +2,7 @@ [中文文档](/solution/0900-0999/0987.Vertical%20Order%20Traversal%20of%20a%20Binary%20Tree/README.md) - + ## Description diff --git a/solution/1000-1099/1058.Minimize Rounding Error to Meet Target/README.md b/solution/1000-1099/1058.Minimize Rounding Error to Meet Target/README.md index 262179b8b9bb3..874d8e18d8d36 100644 --- a/solution/1000-1099/1058.Minimize Rounding Error to Meet Target/README.md +++ b/solution/1000-1099/1058.Minimize Rounding Error to Meet Target/README.md @@ -2,7 +2,7 @@ [English Version](/solution/1000-1099/1058.Minimize%20Rounding%20Error%20to%20Meet%20Target/README_EN.md) - + ## 题目描述 diff --git a/solution/1000-1099/1058.Minimize Rounding Error to Meet Target/README_EN.md b/solution/1000-1099/1058.Minimize Rounding Error to Meet Target/README_EN.md index 4eff7501f62b5..aa984151b7861 100644 --- a/solution/1000-1099/1058.Minimize Rounding Error to Meet Target/README_EN.md +++ b/solution/1000-1099/1058.Minimize Rounding Error to Meet Target/README_EN.md @@ -2,7 +2,7 @@ [中文文档](/solution/1000-1099/1058.Minimize%20Rounding%20Error%20to%20Meet%20Target/README.md) - + ## Description diff --git a/solution/1000-1099/1059.All Paths from Source Lead to Destination/README.md b/solution/1000-1099/1059.All Paths from Source Lead to Destination/README.md index edc28e9db4c06..fa8d5c1ccc757 100644 --- a/solution/1000-1099/1059.All Paths from Source Lead to Destination/README.md +++ b/solution/1000-1099/1059.All Paths from Source Lead to Destination/README.md @@ -2,7 +2,7 @@ [English Version](/solution/1000-1099/1059.All%20Paths%20from%20Source%20Lead%20to%20Destination/README_EN.md) - + ## 题目描述 diff --git a/solution/1000-1099/1059.All Paths from Source Lead to Destination/README_EN.md b/solution/1000-1099/1059.All Paths from Source Lead to Destination/README_EN.md index aa2fd42279d4a..9da94de43433f 100644 --- a/solution/1000-1099/1059.All Paths from Source Lead to Destination/README_EN.md +++ b/solution/1000-1099/1059.All Paths from Source Lead to Destination/README_EN.md @@ -2,7 +2,7 @@ [中文文档](/solution/1000-1099/1059.All%20Paths%20from%20Source%20Lead%20to%20Destination/README.md) - + ## Description diff --git a/solution/1400-1499/1463.Cherry Pickup II/README.md b/solution/1400-1499/1463.Cherry Pickup II/README.md index 60b03e06106cf..7a018726587b2 100644 --- a/solution/1400-1499/1463.Cherry Pickup II/README.md +++ b/solution/1400-1499/1463.Cherry Pickup II/README.md @@ -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)$。 - ```python @@ -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 { @@ -210,13 +208,10 @@ 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 } ``` @@ -224,9 +219,9 @@ func cherryPickup(grid [][]int) int { 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) { @@ -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()); } ``` -### 方法二 +### 方法二:动态规划(空间优化) + +注意到 $f[i][j_1][j_2]$ 的计算只和 $f[i-1][y_1][y_2]$ 有关,因此我们可以使用滚动数组优化空间复杂度,空间复杂度优化后的时间复杂度为 $O(n^2)$。 @@ -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) @@ -382,13 +373,10 @@ 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 } ``` @@ -396,8 +384,8 @@ func cherryPickup(grid [][]int) int { 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) { @@ -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()); } ``` diff --git a/solution/1400-1499/1463.Cherry Pickup II/README_EN.md b/solution/1400-1499/1463.Cherry Pickup II/README_EN.md index c6a8acd25d40a..1d55d12949f1b 100644 --- a/solution/1400-1499/1463.Cherry Pickup II/README_EN.md +++ b/solution/1400-1499/1463.Cherry Pickup II/README_EN.md @@ -60,7 +60,21 @@ Total of cherries: 17 + 11 = 28. ## Solutions -### Solution 1 +### Solution 1: Dynamic Programming + +We define $f[i][j_1][j_2]$ as the maximum number of cherries that can be picked when the two robots are at positions $j_1$ and $j_2$ in the $i$-th row. Initially, $f[0][0][n-1] = grid[0][0] + grid[0][n-1]$, and the other values are $-1$. The answer is $\max_{0 \leq j_1, j_2 < n} f[m-1][j_1][j_2]$. + +Consider $f[i][j_1][j_2]$. If $j_1 \neq j_2$, then the number of cherries that the robots can pick in the $i$-th row is $grid[i][j_1] + grid[i][j_2]$. If $j_1 = j_2$, then the number of cherries that the robots can pick in the $i$-th row is $grid[i][j_1]$. We can enumerate the previous state of the two robots $f[i-1][y1][y2]$, where $y_1, y_2$ are the positions of the two robots in the $(i-1)$-th row, then $y_1 \in \{j_1-1, j_1, j_1+1\}$ and $y_2 \in \{j_2-1, j_2, j_2+1\}$. The state transition equation is as follows: + +$$ +f[i][j_1][j_2] = \max_{y_1 \in \{j_1-1, j_1, j_1+1\}, y_2 \in \{j_2-1, j_2, j_2+1\}} f[i-1][y_1][y_2] + \begin{cases} grid[i][j_1] + grid[i][j_2], & j_1 \neq j_2 \\ grid[i][j_1], & j_1 = j_2 \end{cases} +$$ + +Where $f[i-1][y_1][y_2]$ is ignored when it is $-1$. + +The final answer is $\max_{0 \leq j_1, j_2 < n} f[m-1][j_1][j_2]$. + +The time complexity is $O(m \times n^2)$, and the space complexity is $O(m \times n^2)$. Where $m$ and $n$ are the number of rows and columns of the grid, respectively. @@ -151,7 +165,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 { @@ -181,13 +195,10 @@ 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 } ``` @@ -195,9 +206,9 @@ func cherryPickup(grid [][]int) int { 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) { @@ -213,19 +224,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()); } ``` -### Solution 2 +### Solution 2: Dynamic Programming (Space Optimization) + +Notice that the calculation of $f[i][j_1][j_2]$ is only related to $f[i-1][y_1][y_2]$. Therefore, we can use a rolling array to optimize the space complexity. After optimizing the space complexity, the time complexity is $O(n^2)$. @@ -322,7 +329,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) @@ -353,13 +360,10 @@ 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 } ``` @@ -367,8 +371,8 @@ func cherryPickup(grid [][]int) int { 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) { @@ -385,13 +389,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()); } ``` diff --git a/solution/1400-1499/1463.Cherry Pickup II/Solution.go b/solution/1400-1499/1463.Cherry Pickup II/Solution.go index b373190afd69a..b5cc66b7bf328 100644 --- a/solution/1400-1499/1463.Cherry Pickup II/Solution.go +++ b/solution/1400-1499/1463.Cherry Pickup II/Solution.go @@ -1,4 +1,4 @@ -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 { @@ -28,11 +28,8 @@ 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 } \ No newline at end of file diff --git a/solution/1400-1499/1463.Cherry Pickup II/Solution.ts b/solution/1400-1499/1463.Cherry Pickup II/Solution.ts index 446efa119723e..11eec3863aaa9 100644 --- a/solution/1400-1499/1463.Cherry Pickup II/Solution.ts +++ b/solution/1400-1499/1463.Cherry Pickup II/Solution.ts @@ -1,9 +1,9 @@ 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) { @@ -19,11 +19,5 @@ 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()); } diff --git a/solution/1400-1499/1463.Cherry Pickup II/Solution2.go b/solution/1400-1499/1463.Cherry Pickup II/Solution2.go index f0d29d1fffb48..fd738e2854a79 100644 --- a/solution/1400-1499/1463.Cherry Pickup II/Solution2.go +++ b/solution/1400-1499/1463.Cherry Pickup II/Solution2.go @@ -1,4 +1,4 @@ -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) @@ -29,11 +29,8 @@ 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 } \ No newline at end of file diff --git a/solution/1400-1499/1463.Cherry Pickup II/Solution2.ts b/solution/1400-1499/1463.Cherry Pickup II/Solution2.ts index b523176718041..60d8ebdf2d60a 100644 --- a/solution/1400-1499/1463.Cherry Pickup II/Solution2.ts +++ b/solution/1400-1499/1463.Cherry Pickup II/Solution2.ts @@ -1,8 +1,8 @@ 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) { @@ -19,11 +19,5 @@ 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()); } diff --git a/solution/1500-1599/1525.Number of Good Ways to Split a String/README.md b/solution/1500-1599/1525.Number of Good Ways to Split a String/README.md index 215ea7dc974a4..6b3f74a13b243 100644 --- a/solution/1500-1599/1525.Number of Good Ways to Split a String/README.md +++ b/solution/1500-1599/1525.Number of Good Ways to Split a String/README.md @@ -2,7 +2,7 @@ [English Version](/solution/1500-1599/1525.Number%20of%20Good%20Ways%20to%20Split%20a%20String/README_EN.md) - + ## 题目描述 diff --git a/solution/1500-1599/1525.Number of Good Ways to Split a String/README_EN.md b/solution/1500-1599/1525.Number of Good Ways to Split a String/README_EN.md index f91589eaaaae9..31e0e00e79318 100644 --- a/solution/1500-1599/1525.Number of Good Ways to Split a String/README_EN.md +++ b/solution/1500-1599/1525.Number of Good Ways to Split a String/README_EN.md @@ -2,7 +2,7 @@ [中文文档](/solution/1500-1599/1525.Number%20of%20Good%20Ways%20to%20Split%20a%20String/README.md) - + ## Description diff --git a/solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/README_EN.md b/solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/README_EN.md index 54731a2c1c022..690b686249c53 100644 --- a/solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/README_EN.md +++ b/solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/README_EN.md @@ -6,42 +6,40 @@ ## Description -

    A string is a valid parentheses string (denoted VPS) if it meets one of the following:

    +

    Given a valid parentheses string s, return the nesting depth of s. The nesting depth is the maximum number of nested parentheses.

    - +

     

    +

    Example 1:

    -

    We can similarly define the nesting depth depth(S) of any VPS S as follows:

    +
    +

    Input: s = "(1+(2*3)+((8)/4))+1"

    - +

    Output: 3

    -

    For example, "", "()()", and "()(()())" are VPS's (with nesting depths 0, 1, and 2), and ")(" and "(()" are not VPS's.

    +

    Explanation:

    -

    Given a VPS represented as string s, return the nesting depth of s.

    +

    Digit 8 is inside of 3 nested parentheses in the string.

    +
    -

     

    -

    Example 1:

    +

    Example 2:

    -
    -Input: s = "(1+(2*3)+((8)/4))+1"
    -Output: 3
    -Explanation: Digit 8 is inside of 3 nested parentheses in the string.
    -
    +
    +

    Input: s = "(1)+((2))+(((3)))"

    -

    Example 2:

    +

    Output: 3

    + +

    Explanation:

    + +

    Digit 3 is inside of 3 nested parentheses in the string.

    +
    + +

    Example 3:

    + +
    +

    Input: s = "()(())((()()))"

    -
    -Input: s = "(1)+((2))+(((3)))"
    -Output: 3
    -
    +

    Output: 3

    +

     

    Constraints:

    @@ -49,7 +47,7 @@ ## Solutions diff --git a/solution/1700-1799/1766.Tree of Coprimes/README.md b/solution/1700-1799/1766.Tree of Coprimes/README.md index e80f6bb605acb..cbfee3f05e1cd 100644 --- a/solution/1700-1799/1766.Tree of Coprimes/README.md +++ b/solution/1700-1799/1766.Tree of Coprimes/README.md @@ -2,7 +2,7 @@ [English Version](/solution/1700-1799/1766.Tree%20of%20Coprimes/README_EN.md) - + ## 题目描述 diff --git a/solution/1700-1799/1766.Tree of Coprimes/README_EN.md b/solution/1700-1799/1766.Tree of Coprimes/README_EN.md index 2e1ff4727b2be..c95f14827b951 100644 --- a/solution/1700-1799/1766.Tree of Coprimes/README_EN.md +++ b/solution/1700-1799/1766.Tree of Coprimes/README_EN.md @@ -2,7 +2,7 @@ [中文文档](/solution/1700-1799/1766.Tree%20of%20Coprimes/README.md) - + ## Description diff --git a/solution/1800-1899/1863.Sum of All Subset XOR Totals/README.md b/solution/1800-1899/1863.Sum of All Subset XOR Totals/README.md index c5be794dffe2f..6997cca2cec26 100644 --- a/solution/1800-1899/1863.Sum of All Subset XOR Totals/README.md +++ b/solution/1800-1899/1863.Sum of All Subset XOR Totals/README.md @@ -2,7 +2,7 @@ [English Version](/solution/1800-1899/1863.Sum%20of%20All%20Subset%20XOR%20Totals/README_EN.md) - + ## 题目描述 diff --git a/solution/1800-1899/1863.Sum of All Subset XOR Totals/README_EN.md b/solution/1800-1899/1863.Sum of All Subset XOR Totals/README_EN.md index b6d50544b9d8b..91bde1a1fbd70 100644 --- a/solution/1800-1899/1863.Sum of All Subset XOR Totals/README_EN.md +++ b/solution/1800-1899/1863.Sum of All Subset XOR Totals/README_EN.md @@ -2,7 +2,7 @@ [中文文档](/solution/1800-1899/1863.Sum%20of%20All%20Subset%20XOR%20Totals/README.md) - + ## Description diff --git a/solution/1900-1999/1928.Minimum Cost to Reach Destination in Time/README.md b/solution/1900-1999/1928.Minimum Cost to Reach Destination in Time/README.md index 951671c9c4e36..bbde954e1cf1c 100644 --- a/solution/1900-1999/1928.Minimum Cost to Reach Destination in Time/README.md +++ b/solution/1900-1999/1928.Minimum Cost to Reach Destination in Time/README.md @@ -2,7 +2,7 @@ [English Version](/solution/1900-1999/1928.Minimum%20Cost%20to%20Reach%20Destination%20in%20Time/README_EN.md) - + ## 题目描述 diff --git a/solution/1900-1999/1928.Minimum Cost to Reach Destination in Time/README_EN.md b/solution/1900-1999/1928.Minimum Cost to Reach Destination in Time/README_EN.md index 531a934a00a23..75357ff01af68 100644 --- a/solution/1900-1999/1928.Minimum Cost to Reach Destination in Time/README_EN.md +++ b/solution/1900-1999/1928.Minimum Cost to Reach Destination in Time/README_EN.md @@ -2,7 +2,7 @@ [中文文档](/solution/1900-1999/1928.Minimum%20Cost%20to%20Reach%20Destination%20in%20Time/README.md) - + ## Description diff --git a/solution/1900-1999/1995.Count Special Quadruplets/README.md b/solution/1900-1999/1995.Count Special Quadruplets/README.md index 1f8b1bf577e22..b98e8120d1c30 100644 --- a/solution/1900-1999/1995.Count Special Quadruplets/README.md +++ b/solution/1900-1999/1995.Count Special Quadruplets/README.md @@ -2,7 +2,7 @@ [English Version](/solution/1900-1999/1995.Count%20Special%20Quadruplets/README_EN.md) - + ## 题目描述 diff --git a/solution/1900-1999/1995.Count Special Quadruplets/README_EN.md b/solution/1900-1999/1995.Count Special Quadruplets/README_EN.md index a0cce57ad880f..beb9b709a11e1 100644 --- a/solution/1900-1999/1995.Count Special Quadruplets/README_EN.md +++ b/solution/1900-1999/1995.Count Special Quadruplets/README_EN.md @@ -2,7 +2,7 @@ [中文文档](/solution/1900-1999/1995.Count%20Special%20Quadruplets/README.md) - + ## Description diff --git a/solution/2000-2099/2015.Average Height of Buildings in Each Segment/README.md b/solution/2000-2099/2015.Average Height of Buildings in Each Segment/README.md index 41b0bca6302a0..50675af8d0757 100644 --- a/solution/2000-2099/2015.Average Height of Buildings in Each Segment/README.md +++ b/solution/2000-2099/2015.Average Height of Buildings in Each Segment/README.md @@ -8,7 +8,7 @@ -

    一条完全笔直的街道由一条数字线表示。街道上有建筑物,由二维整数阵列 buildings 表示,其中 buildings[i] = [starti, endi, heighti]。这意味着在 半封闭的位置[starti,endi] 有一座高度为 heighti 的建筑。
    +

    一条完全笔直的街道由一条数字线表示。街道上有建筑物,由二维整数阵列 buildings 表示,其中 buildings[i] = [starti, endi, heighti]。这意味着在 半封闭的位置[starti,endi) 有一座高度为 heighti 的建筑。
    你想用 最少 数量的非重叠 部分描述 街道上建筑物的高度。街道可以用2D整数数组 street 来表示,其中 street[j] = [leftj, rightj, averagej] 描述了道路的 半封闭区域 [leftj, rightj) ,该段中建筑物的 平均 高度为 averagej