From a7d1e560dcc85ac3fb3c7a82e10dc2d8b435186b Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 13 Mar 2025 11:22:25 +0800 Subject: [PATCH 1/4] feat: add solutions to lc problem: No.1959 (#4155) No.1959.Minimum Total Space Wasted With K Resizing Operations --- .../README.md | 88 +++++++++++++++++++ .../README_EN.md | 86 +++++++++++++++++- .../Solution.rs | 30 +++++++ .../Solution.ts | 29 ++++++ 4 files changed, 231 insertions(+), 2 deletions(-) create mode 100644 solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/Solution.rs create mode 100644 solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/Solution.ts diff --git a/solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/README.md b/solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/README.md index 11ce72783a078..c95c86c83c086 100644 --- a/solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/README.md +++ b/solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/README.md @@ -74,6 +74,25 @@ tags: ### 方法一:动态规划 +题目等价于我们将数组 $\textit{nums}$ 分成 $k + 1$ 段,那么每一段的浪费空间为该段的最大值乘以该段的长度减去该段的元素之和。我们累加每一段的浪费空间,即可得到总浪费空间。我们将 $k$ 加 $1$,那么就相当于将数组分成 $k$ 段。 + +因此,我们定义数组 $\textit{g}[i][j]$ 表示 $\textit{nums}[i..j]$ 的最大值乘以 $\textit{nums}[i..j]$ 的长度减去 $\textit{nums}[i..j]$ 的元素之和。我们在 $[0, n)$ 的范围内枚举 $i$,在 $[i, n)$ 的范围内枚举 $j$,用一个变量 $s$ 维护 $\textit{nums}[i..j]$ 的元素之和,用一个变量 $\textit{mx}$ 维护 $\textit{nums}[i..j]$ 的最大值,那么我们可以得到: + +$$ +\textit{g}[i][j] = \textit{mx} \times (j - i + 1) - s +$$ + +接下来,我们定义 $\textit{f}[i][j]$ 表示前 $i$ 个元素分成 $j$ 段的最小浪费空间。我们初始化 $\textit{f}[0][0] = 0$,其余位置初始化为无穷大。我们在 $[1, n]$ 的范围内枚举 $i$,在 $[1, k]$ 的范围内枚举 $j$,然后我们枚举前 $j - 1$ 段的最后一个元素 $h$,那么有: + +$$ +\textit{f}[i][j] = \min(\textit{f}[i][j], \ +\textit{f}[h][j - 1] + \textit{g}[h][i - 1]) +$$ + +最终答案为 $\textit{f}[n][k]$。 + +时间复杂度 $O(n^2 \times k)$,空间复杂度 $O(n \times (n + k))$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 + #### Python3 @@ -203,6 +222,75 @@ func minSpaceWastedKResizing(nums []int, k int) int { } ``` +#### TypeScript + +```ts +function minSpaceWastedKResizing(nums: number[], k: number): number { + k += 1; + const n = nums.length; + const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0)); + + for (let i = 0; i < n; i++) { + let s = 0, + mx = 0; + for (let j = i; j < n; j++) { + s += nums[j]; + mx = Math.max(mx, nums[j]); + g[i][j] = mx * (j - i + 1) - s; + } + } + + const inf = Number.POSITIVE_INFINITY; + const f: number[][] = Array.from({ length: n + 1 }, () => Array(k + 1).fill(inf)); + f[0][0] = 0; + + for (let i = 1; i <= n; i++) { + for (let j = 1; j <= k; j++) { + for (let h = 0; h < i; h++) { + f[i][j] = Math.min(f[i][j], f[h][j - 1] + g[h][i - 1]); + } + } + } + + return f[n][k]; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn min_space_wasted_k_resizing(nums: Vec, k: i32) -> i32 { + let mut k = k + 1; + let n = nums.len(); + let mut g = vec![vec![0; n]; n]; + + for i in 0..n { + let (mut s, mut mx) = (0, 0); + for j in i..n { + s += nums[j]; + mx = mx.max(nums[j]); + g[i][j] = mx * (j as i32 - i as i32 + 1) - s; + } + } + + let inf = 0x3f3f3f3f; + let mut f = vec![vec![inf; (k + 1) as usize]; n + 1]; + f[0][0] = 0; + + for i in 1..=n { + for j in 1..=k as usize { + for h in 0..i { + f[i][j] = f[i][j].min(f[h][j - 1] + g[h][i - 1]); + } + } + } + + f[n][k as usize] + } +} +``` + diff --git a/solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/README_EN.md b/solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/README_EN.md index 90fc97567380f..eb664610b9299 100644 --- a/solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/README_EN.md +++ b/solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/README_EN.md @@ -44,7 +44,7 @@ The total wasted space is (20 - 10) + (20 - 20) = 10. Input: nums = [10,20,30], k = 1 Output: 10 Explanation: size = [20,20,30]. -We can set the initial size to be 20 and resize to 30 at time 2. +We can set the initial size to be 20 and resize to 30 at time 2. The total wasted space is (20 - 10) + (20 - 20) + (30 - 30) = 10. @@ -73,7 +73,20 @@ The total wasted space is (10 - 10) + (20 - 20) + (20 - 15) + (30 - 30) + (30 - -### Solution 1 +Solution 1: Dynamic Programming +The problem is equivalent to dividing the array $\textit{nums}$ into $k + 1$ segments. The wasted space for each segment is the maximum value of that segment multiplied by the length of the segment minus the sum of the elements in that segment. By summing the wasted space of each segment, we get the total wasted space. By adding 1 to $k$, we are effectively dividing the array into $k$ segments. + +Therefore, we define an array $\textit{g}[i][j]$ to represent the wasted space for the segment $\textit{nums}[i..j]$, which is the maximum value of $\textit{nums}[i..j]$ multiplied by the length of $\textit{nums}[i..j]$ minus the sum of the elements in $\textit{nums}[i..j]$. We iterate over $i$ in the range $[0, n)$ and $j$ in the range $[i, n)$, using a variable $s$ to maintain the sum of the elements in $\textit{nums}[i..j]$ and a variable $\textit{mx}$ to maintain the maximum value of $\textit{nums}[i..j]$. Then we can get: + +$$ \textit{g}[i][j] = \textit{mx} \times (j - i + 1) - s $$ + +Next, we define $\textit{f}[i][j]$ to represent the minimum wasted space for dividing the first $i$ elements into $j$ segments. We initialize $\textit{f}[0][0] = 0$ and the other positions to infinity. We iterate over $i$ in the range $[1, n]$ and $j$ in the range $[1, k]$, then we iterate over the last element $h$ of the previous $j - 1$ segments. Then we have: + +$$ \textit{f}[i][j] = \min(\textit{f}[i][j], \textit{f}[h][j - 1] + \textit{g}[h][i - 1]) $$ + +The final answer is $\textit{f}[n][k]$. + +The time complexity is $O(n^2 \times k)$, and the space complexity is $O(n \times (n + k))$. Where $n$ is the length of the array $\textit{nums}$. @@ -204,6 +217,75 @@ func minSpaceWastedKResizing(nums []int, k int) int { } ``` +#### TypeScript + +```ts +function minSpaceWastedKResizing(nums: number[], k: number): number { + k += 1; + const n = nums.length; + const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0)); + + for (let i = 0; i < n; i++) { + let s = 0, + mx = 0; + for (let j = i; j < n; j++) { + s += nums[j]; + mx = Math.max(mx, nums[j]); + g[i][j] = mx * (j - i + 1) - s; + } + } + + const inf = Number.POSITIVE_INFINITY; + const f: number[][] = Array.from({ length: n + 1 }, () => Array(k + 1).fill(inf)); + f[0][0] = 0; + + for (let i = 1; i <= n; i++) { + for (let j = 1; j <= k; j++) { + for (let h = 0; h < i; h++) { + f[i][j] = Math.min(f[i][j], f[h][j - 1] + g[h][i - 1]); + } + } + } + + return f[n][k]; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn min_space_wasted_k_resizing(nums: Vec, k: i32) -> i32 { + let mut k = k + 1; + let n = nums.len(); + let mut g = vec![vec![0; n]; n]; + + for i in 0..n { + let (mut s, mut mx) = (0, 0); + for j in i..n { + s += nums[j]; + mx = mx.max(nums[j]); + g[i][j] = mx * (j as i32 - i as i32 + 1) - s; + } + } + + let inf = 0x3f3f3f3f; + let mut f = vec![vec![inf; (k + 1) as usize]; n + 1]; + f[0][0] = 0; + + for i in 1..=n { + for j in 1..=k as usize { + for h in 0..i { + f[i][j] = f[i][j].min(f[h][j - 1] + g[h][i - 1]); + } + } + } + + f[n][k as usize] + } +} +``` + diff --git a/solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/Solution.rs b/solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/Solution.rs new file mode 100644 index 0000000000000..b6d7f132938be --- /dev/null +++ b/solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/Solution.rs @@ -0,0 +1,30 @@ +impl Solution { + pub fn min_space_wasted_k_resizing(nums: Vec, k: i32) -> i32 { + let mut k = k + 1; + let n = nums.len(); + let mut g = vec![vec![0; n]; n]; + + for i in 0..n { + let (mut s, mut mx) = (0, 0); + for j in i..n { + s += nums[j]; + mx = mx.max(nums[j]); + g[i][j] = mx * (j as i32 - i as i32 + 1) - s; + } + } + + let inf = 0x3f3f3f3f; + let mut f = vec![vec![inf; (k + 1) as usize]; n + 1]; + f[0][0] = 0; + + for i in 1..=n { + for j in 1..=k as usize { + for h in 0..i { + f[i][j] = f[i][j].min(f[h][j - 1] + g[h][i - 1]); + } + } + } + + f[n][k as usize] + } +} diff --git a/solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/Solution.ts b/solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/Solution.ts new file mode 100644 index 0000000000000..68a578a0e67d9 --- /dev/null +++ b/solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/Solution.ts @@ -0,0 +1,29 @@ +function minSpaceWastedKResizing(nums: number[], k: number): number { + k += 1; + const n = nums.length; + const g: number[][] = Array.from({ length: n }, () => Array(n).fill(0)); + + for (let i = 0; i < n; i++) { + let s = 0, + mx = 0; + for (let j = i; j < n; j++) { + s += nums[j]; + mx = Math.max(mx, nums[j]); + g[i][j] = mx * (j - i + 1) - s; + } + } + + const inf = Number.POSITIVE_INFINITY; + const f: number[][] = Array.from({ length: n + 1 }, () => Array(k + 1).fill(inf)); + f[0][0] = 0; + + for (let i = 1; i <= n; i++) { + for (let j = 1; j <= k; j++) { + for (let h = 0; h < i; h++) { + f[i][j] = Math.min(f[i][j], f[h][j - 1] + g[h][i - 1]); + } + } + } + + return f[n][k]; +} From 4eba2690e6de83093c7176ccf65434316b9e9626 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 13 Mar 2025 12:52:29 +0800 Subject: [PATCH 2/4] feat: add solutions to lc problem: No.2829 (#4156) No.2829.Determine the Minimum Sum of a k-avoiding Array --- .../README.md | 49 +++++++++++++------ .../README_EN.md | 47 ++++++++++++------ .../Solution.cpp | 7 ++- .../Solution.go | 6 +-- .../Solution.java | 7 ++- .../Solution.py | 2 +- .../Solution.rs | 17 +++++++ .../Solution.ts | 5 +- 8 files changed, 96 insertions(+), 44 deletions(-) create mode 100644 solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.rs diff --git a/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/README.md b/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/README.md index 3d9cd834ac3f9..98bb5154a8cae 100644 --- a/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/README.md +++ b/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/README.md @@ -42,7 +42,7 @@ tags: 输入:n = 2, k = 6 输出:3 解释:可以构造数组 [1,2] ,其元素总和为 3 。 -可以证明不存在总和小于 3 的 k-avoiding 数组。 +可以证明不存在总和小于 3 的 k-avoiding 数组。

 

@@ -61,9 +61,9 @@ tags: ### 方法一:贪心 + 模拟 -我们从正整数 $i=1$ 开始,依次判断 $i$ 是否可以加入数组中,如果可以加入,则将 $i$ 加入数组中,累加到答案中,然后将 $k-i$ 置为已访问,表示 $k-i$ 不能加入数组中。循环直到数组长度为 $n$。 +我们从正整数 $i = 1$ 开始,依次判断 $i$ 是否可以加入数组中,如果可以加入,则将 $i$ 加入数组中,累加到答案中,然后将 $k - i$ 置为已访问,表示 $k-i$ 不能加入数组中。循环直到数组长度为 $n$。 -时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 为数组长度。 +时间复杂度 $O(n + k)$,空间复杂度 $O(n + k)$。其中 $n$ 为数组长度。 @@ -77,9 +77,9 @@ class Solution: for _ in range(n): while i in vis: i += 1 - vis.add(i) vis.add(k - i) s += i + i += 1 return s ``` @@ -89,16 +89,15 @@ class Solution: class Solution { public int minimumSum(int n, int k) { int s = 0, i = 1; - boolean[] vis = new boolean[k + n * n + 1]; + boolean[] vis = new boolean[n + k + 1]; while (n-- > 0) { while (vis[i]) { ++i; } - vis[i] = true; if (k >= i) { vis[k - i] = true; } - s += i; + s += i++; } return s; } @@ -112,17 +111,16 @@ class Solution { public: int minimumSum(int n, int k) { int s = 0, i = 1; - bool vis[k + n * n + 1]; + bool vis[n + k + 1]; memset(vis, false, sizeof(vis)); while (n--) { while (vis[i]) { ++i; } - vis[i] = true; if (k >= i) { vis[k - i] = true; } - s += i; + s += i++; } return s; } @@ -134,16 +132,16 @@ public: ```go func minimumSum(n int, k int) int { s, i := 0, 1 - vis := make([]bool, k+n*n+1) + vis := make([]bool, n+k+1) for ; n > 0; n-- { for vis[i] { i++ } - vis[i] = true if k >= i { vis[k-i] = true } s += i + i++ } return s } @@ -155,21 +153,42 @@ func minimumSum(n int, k int) int { function minimumSum(n: number, k: number): number { let s = 0; let i = 1; - const vis: boolean[] = Array(n * n + k + 1); + const vis: boolean[] = Array(n + k + 1).fill(false); while (n--) { while (vis[i]) { ++i; } - vis[i] = true; if (k >= i) { vis[k - i] = true; } - s += i; + s += i++; } return s; } ``` +#### Rust + +```rust +impl Solution { + pub fn minimum_sum(n: i32, k: i32) -> i32 { + let (mut s, mut i) = (0, 1); + let mut vis = std::collections::HashSet::new(); + + for _ in 0..n { + while vis.contains(&i) { + i += 1; + } + vis.insert(k - i); + s += i; + i += 1; + } + + s + } +} +``` + diff --git a/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/README_EN.md b/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/README_EN.md index 9d36054d96cac..3da1ed4c0cbff 100644 --- a/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/README_EN.md +++ b/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/README_EN.md @@ -59,9 +59,9 @@ It can be proven that there is no k-avoiding array with a sum less than 3. ### Solution 1: Greedy + Simulation -We start from the positive integer $i=1$, and judge whether $i$ can be added to the array in turn. If it can be added, we add $i$ to the array, accumulate it to the answer, and then mark $k-i$ as visited, indicating that $k-i$ cannot be added to the array. The loop continues until the length of the array is $n$. +Starting from the positive integer $i = 1$, we sequentially determine if $i$ can be added to the array. If it can be added, we add $i$ to the array, accumulate it to the answer, and then mark $k - i$ as visited, indicating that $k-i$ cannot be added to the array. We continue this process until the array's length reaches $n$. -The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the length of the array. +The time complexity is $O(n + k)$, and the space complexity is $O(n + k)$. Where $n$ is the length of the array. @@ -75,9 +75,9 @@ class Solution: for _ in range(n): while i in vis: i += 1 - vis.add(i) vis.add(k - i) s += i + i += 1 return s ``` @@ -87,16 +87,15 @@ class Solution: class Solution { public int minimumSum(int n, int k) { int s = 0, i = 1; - boolean[] vis = new boolean[k + n * n + 1]; + boolean[] vis = new boolean[n + k + 1]; while (n-- > 0) { while (vis[i]) { ++i; } - vis[i] = true; if (k >= i) { vis[k - i] = true; } - s += i; + s += i++; } return s; } @@ -110,17 +109,16 @@ class Solution { public: int minimumSum(int n, int k) { int s = 0, i = 1; - bool vis[k + n * n + 1]; + bool vis[n + k + 1]; memset(vis, false, sizeof(vis)); while (n--) { while (vis[i]) { ++i; } - vis[i] = true; if (k >= i) { vis[k - i] = true; } - s += i; + s += i++; } return s; } @@ -132,16 +130,16 @@ public: ```go func minimumSum(n int, k int) int { s, i := 0, 1 - vis := make([]bool, k+n*n+1) + vis := make([]bool, n+k+1) for ; n > 0; n-- { for vis[i] { i++ } - vis[i] = true if k >= i { vis[k-i] = true } s += i + i++ } return s } @@ -153,21 +151,42 @@ func minimumSum(n int, k int) int { function minimumSum(n: number, k: number): number { let s = 0; let i = 1; - const vis: boolean[] = Array(n * n + k + 1); + const vis: boolean[] = Array(n + k + 1).fill(false); while (n--) { while (vis[i]) { ++i; } - vis[i] = true; if (k >= i) { vis[k - i] = true; } - s += i; + s += i++; } return s; } ``` +#### Rust + +```rust +impl Solution { + pub fn minimum_sum(n: i32, k: i32) -> i32 { + let (mut s, mut i) = (0, 1); + let mut vis = std::collections::HashSet::new(); + + for _ in 0..n { + while vis.contains(&i) { + i += 1; + } + vis.insert(k - i); + s += i; + i += 1; + } + + s + } +} +``` + diff --git a/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.cpp b/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.cpp index 3451c86c770f7..4a9b148e5c893 100644 --- a/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.cpp +++ b/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.cpp @@ -2,18 +2,17 @@ class Solution { public: int minimumSum(int n, int k) { int s = 0, i = 1; - bool vis[k + n * n + 1]; + bool vis[n + k + 1]; memset(vis, false, sizeof(vis)); while (n--) { while (vis[i]) { ++i; } - vis[i] = true; if (k >= i) { vis[k - i] = true; } - s += i; + s += i++; } return s; } -}; \ No newline at end of file +}; diff --git a/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.go b/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.go index 0815dc8d621b8..1b7eb8c59b32f 100644 --- a/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.go +++ b/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.go @@ -1,15 +1,15 @@ func minimumSum(n int, k int) int { s, i := 0, 1 - vis := make([]bool, k+n*n+1) + vis := make([]bool, n+k+1) for ; n > 0; n-- { for vis[i] { i++ } - vis[i] = true if k >= i { vis[k-i] = true } s += i + i++ } return s -} \ No newline at end of file +} diff --git a/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.java b/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.java index b1d79af8836e9..9795f45a1210a 100644 --- a/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.java +++ b/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.java @@ -1,17 +1,16 @@ class Solution { public int minimumSum(int n, int k) { int s = 0, i = 1; - boolean[] vis = new boolean[k + n * n + 1]; + boolean[] vis = new boolean[n + k + 1]; while (n-- > 0) { while (vis[i]) { ++i; } - vis[i] = true; if (k >= i) { vis[k - i] = true; } - s += i; + s += i++; } return s; } -} \ No newline at end of file +} diff --git a/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.py b/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.py index a6632ca525725..e105ff6cfc42d 100644 --- a/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.py +++ b/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.py @@ -5,7 +5,7 @@ def minimumSum(self, n: int, k: int) -> int: for _ in range(n): while i in vis: i += 1 - vis.add(i) vis.add(k - i) s += i + i += 1 return s diff --git a/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.rs b/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.rs new file mode 100644 index 0000000000000..6684329e08f7c --- /dev/null +++ b/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.rs @@ -0,0 +1,17 @@ +impl Solution { + pub fn minimum_sum(n: i32, k: i32) -> i32 { + let (mut s, mut i) = (0, 1); + let mut vis = std::collections::HashSet::new(); + + for _ in 0..n { + while vis.contains(&i) { + i += 1; + } + vis.insert(k - i); + s += i; + i += 1; + } + + s + } +} diff --git a/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.ts b/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.ts index 75407c9881b83..f664e6087529c 100644 --- a/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.ts +++ b/solution/2800-2899/2829.Determine the Minimum Sum of a k-avoiding Array/Solution.ts @@ -1,16 +1,15 @@ function minimumSum(n: number, k: number): number { let s = 0; let i = 1; - const vis: boolean[] = Array(n * n + k + 1); + const vis: boolean[] = Array(n + k + 1).fill(false); while (n--) { while (vis[i]) { ++i; } - vis[i] = true; if (k >= i) { vis[k - i] = true; } - s += i; + s += i++; } return s; } From f104c69e330a1b8d3823870dc682c42cb66e9bcc Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 13 Mar 2025 15:05:20 +0800 Subject: [PATCH 3/4] feat: add solutions to lc problem: No.0766 (#4157) No.0766.Toeplitz Matrix --- .../0700-0799/0766.Toeplitz Matrix/README.md | 57 +++++++++++++++---- .../0766.Toeplitz Matrix/README_EN.md | 55 +++++++++++++++--- .../0766.Toeplitz Matrix/Solution.js | 5 +- .../0766.Toeplitz Matrix/Solution.py | 10 ++-- .../0766.Toeplitz Matrix/Solution.rs | 13 +++++ .../0766.Toeplitz Matrix/Solution.ts | 11 ++++ 6 files changed, 122 insertions(+), 29 deletions(-) create mode 100644 solution/0700-0799/0766.Toeplitz Matrix/Solution.rs create mode 100644 solution/0700-0799/0766.Toeplitz Matrix/Solution.ts diff --git a/solution/0700-0799/0766.Toeplitz Matrix/README.md b/solution/0700-0799/0766.Toeplitz Matrix/README.md index 8b7e247b86c0c..7912a00f976c1 100644 --- a/solution/0700-0799/0766.Toeplitz Matrix/README.md +++ b/solution/0700-0799/0766.Toeplitz Matrix/README.md @@ -29,8 +29,8 @@ tags: 输入:matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]] 输出:true 解释: -在上述矩阵中, 其对角线为: -"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]"。 +在上述矩阵中, 其对角线为: +"[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]"。 各条对角线上的所有元素均相同, 因此答案是 True 。 @@ -70,9 +70,9 @@ tags: ### 方法一:一次遍历 -遍历矩阵,若出现元素与其左上角的元素不等的情况,返回 `false`。否则,遍历结束后返回 `true`。 +根据题目描述,托普利茨矩阵的特点是:矩阵中每个元素都与其左上角的元素相等。因此,我们只需要遍历矩阵中的每个元素,检查它是否与左上角的元素相等即可。 -时间复杂度 $O(m \times n)$,空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数。 +时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数。空间复杂度 $O(1)$。 @@ -82,11 +82,11 @@ tags: class Solution: def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool: m, n = len(matrix), len(matrix[0]) - return all( - matrix[i][j] == matrix[i - 1][j - 1] - for i in range(1, m) - for j in range(1, n) - ) + for i in range(1, m): + for j in range(1, n): + if matrix[i][j] != matrix[i - 1][j - 1]: + return False + return True ``` #### Java @@ -142,6 +142,40 @@ func isToeplitzMatrix(matrix [][]int) bool { } ``` +#### TypeScript + +```ts +function isToeplitzMatrix(matrix: number[][]): boolean { + const [m, n] = [matrix.length, matrix[0].length]; + for (let i = 1; i < m; ++i) { + for (let j = 1; j < n; ++j) { + if (matrix[i][j] !== matrix[i - 1][j - 1]) { + return false; + } + } + } + return true; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn is_toeplitz_matrix(matrix: Vec>) -> bool { + let (m, n) = (matrix.len(), matrix[0].len()); + for i in 1..m { + for j in 1..n { + if matrix[i][j] != matrix[i - 1][j - 1] { + return false; + } + } + } + true + } +} +``` + #### JavaScript ```js @@ -150,11 +184,10 @@ func isToeplitzMatrix(matrix [][]int) bool { * @return {boolean} */ var isToeplitzMatrix = function (matrix) { - const m = matrix.length; - const n = matrix[0].length; + const [m, n] = [matrix.length, matrix[0].length]; for (let i = 1; i < m; ++i) { for (let j = 1; j < n; ++j) { - if (matrix[i][j] != matrix[i - 1][j - 1]) { + if (matrix[i][j] !== matrix[i - 1][j - 1]) { return false; } } diff --git a/solution/0700-0799/0766.Toeplitz Matrix/README_EN.md b/solution/0700-0799/0766.Toeplitz Matrix/README_EN.md index 0de95666d2991..269acd9ecdaaa 100644 --- a/solution/0700-0799/0766.Toeplitz Matrix/README_EN.md +++ b/solution/0700-0799/0766.Toeplitz Matrix/README_EN.md @@ -66,7 +66,11 @@ The diagonal "[1, 2]" has different elements. -### Solution 1 +### Solution 1: Single Traversal + +According to the problem description, the characteristic of a Toeplitz matrix is that each element is equal to the element in its upper left corner. Therefore, we only need to iterate through each element in the matrix and check if it is equal to the element in its upper left corner. + +The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. The space complexity is $O(1)$. @@ -76,11 +80,11 @@ The diagonal "[1, 2]" has different elements. class Solution: def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool: m, n = len(matrix), len(matrix[0]) - return all( - matrix[i][j] == matrix[i - 1][j - 1] - for i in range(1, m) - for j in range(1, n) - ) + for i in range(1, m): + for j in range(1, n): + if matrix[i][j] != matrix[i - 1][j - 1]: + return False + return True ``` #### Java @@ -136,6 +140,40 @@ func isToeplitzMatrix(matrix [][]int) bool { } ``` +#### TypeScript + +```ts +function isToeplitzMatrix(matrix: number[][]): boolean { + const [m, n] = [matrix.length, matrix[0].length]; + for (let i = 1; i < m; ++i) { + for (let j = 1; j < n; ++j) { + if (matrix[i][j] !== matrix[i - 1][j - 1]) { + return false; + } + } + } + return true; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn is_toeplitz_matrix(matrix: Vec>) -> bool { + let (m, n) = (matrix.len(), matrix[0].len()); + for i in 1..m { + for j in 1..n { + if matrix[i][j] != matrix[i - 1][j - 1] { + return false; + } + } + } + true + } +} +``` + #### JavaScript ```js @@ -144,11 +182,10 @@ func isToeplitzMatrix(matrix [][]int) bool { * @return {boolean} */ var isToeplitzMatrix = function (matrix) { - const m = matrix.length; - const n = matrix[0].length; + const [m, n] = [matrix.length, matrix[0].length]; for (let i = 1; i < m; ++i) { for (let j = 1; j < n; ++j) { - if (matrix[i][j] != matrix[i - 1][j - 1]) { + if (matrix[i][j] !== matrix[i - 1][j - 1]) { return false; } } diff --git a/solution/0700-0799/0766.Toeplitz Matrix/Solution.js b/solution/0700-0799/0766.Toeplitz Matrix/Solution.js index 7b23d487032ef..81cab524dde42 100644 --- a/solution/0700-0799/0766.Toeplitz Matrix/Solution.js +++ b/solution/0700-0799/0766.Toeplitz Matrix/Solution.js @@ -3,11 +3,10 @@ * @return {boolean} */ var isToeplitzMatrix = function (matrix) { - const m = matrix.length; - const n = matrix[0].length; + const [m, n] = [matrix.length, matrix[0].length]; for (let i = 1; i < m; ++i) { for (let j = 1; j < n; ++j) { - if (matrix[i][j] != matrix[i - 1][j - 1]) { + if (matrix[i][j] !== matrix[i - 1][j - 1]) { return false; } } diff --git a/solution/0700-0799/0766.Toeplitz Matrix/Solution.py b/solution/0700-0799/0766.Toeplitz Matrix/Solution.py index 53f7c0f033a66..4f5f378d23848 100644 --- a/solution/0700-0799/0766.Toeplitz Matrix/Solution.py +++ b/solution/0700-0799/0766.Toeplitz Matrix/Solution.py @@ -1,8 +1,8 @@ class Solution: def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool: m, n = len(matrix), len(matrix[0]) - return all( - matrix[i][j] == matrix[i - 1][j - 1] - for i in range(1, m) - for j in range(1, n) - ) + for i in range(1, m): + for j in range(1, n): + if matrix[i][j] != matrix[i - 1][j - 1]: + return False + return True diff --git a/solution/0700-0799/0766.Toeplitz Matrix/Solution.rs b/solution/0700-0799/0766.Toeplitz Matrix/Solution.rs new file mode 100644 index 0000000000000..f033d3e195634 --- /dev/null +++ b/solution/0700-0799/0766.Toeplitz Matrix/Solution.rs @@ -0,0 +1,13 @@ +impl Solution { + pub fn is_toeplitz_matrix(matrix: Vec>) -> bool { + let (m, n) = (matrix.len(), matrix[0].len()); + for i in 1..m { + for j in 1..n { + if matrix[i][j] != matrix[i - 1][j - 1] { + return false; + } + } + } + true + } +} diff --git a/solution/0700-0799/0766.Toeplitz Matrix/Solution.ts b/solution/0700-0799/0766.Toeplitz Matrix/Solution.ts new file mode 100644 index 0000000000000..cbe193e8f0ff7 --- /dev/null +++ b/solution/0700-0799/0766.Toeplitz Matrix/Solution.ts @@ -0,0 +1,11 @@ +function isToeplitzMatrix(matrix: number[][]): boolean { + const [m, n] = [matrix.length, matrix[0].length]; + for (let i = 1; i < m; ++i) { + for (let j = 1; j < n; ++j) { + if (matrix[i][j] !== matrix[i - 1][j - 1]) { + return false; + } + } + } + return true; +} From 8e1dd6b8d1e5e7f98767ecf880e80f142bad5d7f Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 13 Mar 2025 16:14:32 +0800 Subject: [PATCH 4/4] feat: add solutions to lc problem: No.1062 (#4158) No.1062.Longest Repeating Substring --- .../README.md | 114 +++++++++++------ .../README_EN.md | 121 +++++++++++++----- .../Solution.cpp | 15 ++- .../Solution.go | 26 ++-- .../Solution.java | 12 +- .../Solution.py | 10 +- .../Solution.rs | 18 +++ .../Solution.ts | 14 ++ 8 files changed, 232 insertions(+), 98 deletions(-) create mode 100644 solution/1000-1099/1062.Longest Repeating Substring/Solution.rs create mode 100644 solution/1000-1099/1062.Longest Repeating Substring/Solution.ts diff --git a/solution/1000-1099/1062.Longest Repeating Substring/README.md b/solution/1000-1099/1062.Longest Repeating Substring/README.md index 12a7cb773e4c3..aaed274abecbf 100644 --- a/solution/1000-1099/1062.Longest Repeating Substring/README.md +++ b/solution/1000-1099/1062.Longest Repeating Substring/README.md @@ -66,20 +66,21 @@ tags: ### 方法一:动态规划 -定义 $dp[i][j]$ 表示以 $s[i]$ 和 $s[j]$ 结尾的最长重复子串 🔒 的长度。状态转移方程为: +我们定义 $f[i][j]$ 表示以 $s[i]$ 和 $s[j]$ 结尾的最长重复子串的长度,初始时 $f[i][j]=0$。 + +我们在 $[1, n)$ 的区间内枚举 $i$,在 $[0, i)$ 的区间内枚举 $j$,如果 $s[i]=s[j]$,那么有: $$ -dp[i][j]= +f[i][j]= \begin{cases} -dp[i-1][j-1]+1, & i>0 \cap s[i]=s[j] \\ -1, & i=0 \cap s[i]=s[j] \\ -0, & s[i] \neq s[j] +f[i-1][j-1]+1, & j>0 \\ +1, & j=0 \end{cases} $$ -时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。 +我们求出所有 $f[i][j]$ 的最大值即为答案。 -其中 $n$ 为字符串 $s$ 的长度。 +时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 为字符串 $s$ 的长度。 相似题目: @@ -93,13 +94,13 @@ $$ class Solution: def longestRepeatingSubstring(self, s: str) -> int: n = len(s) - dp = [[0] * n for _ in range(n)] + f = [[0] * n for _ in range(n)] ans = 0 - for i in range(n): - for j in range(i + 1, n): + for i in range(1, n): + for j in range(i): if s[i] == s[j]: - dp[i][j] = dp[i - 1][j - 1] + 1 if i else 1 - ans = max(ans, dp[i][j]) + f[i][j] = 1 + (f[i - 1][j - 1] if j else 0) + ans = max(ans, f[i][j]) return ans ``` @@ -109,13 +110,13 @@ class Solution: class Solution { public int longestRepeatingSubstring(String s) { int n = s.length(); + int[][] f = new int[n][n]; int ans = 0; - int[][] dp = new int[n][n]; - for (int i = 0; i < n; ++i) { - for (int j = i + 1; j < n; ++j) { + for (int i = 1; i < n; ++i) { + for (int j = 0; j < i; ++j) { if (s.charAt(i) == s.charAt(j)) { - dp[i][j] = i > 0 ? dp[i - 1][j - 1] + 1 : 1; - ans = Math.max(ans, dp[i][j]); + f[i][j] = 1 + (j > 0 ? f[i - 1][j - 1] : 0); + ans = Math.max(ans, f[i][j]); } } } @@ -130,14 +131,15 @@ class Solution { class Solution { public: int longestRepeatingSubstring(string s) { - int n = s.size(); - vector> dp(n, vector(n)); + int n = s.length(); + int f[n][n]; + memset(f, 0, sizeof(f)); int ans = 0; - for (int i = 0; i < n; ++i) { - for (int j = i + 1; j < n; ++j) { + for (int i = 1; i < n; ++i) { + for (int j = 0; j < i; ++j) { if (s[i] == s[j]) { - dp[i][j] = i ? dp[i - 1][j - 1] + 1 : 1; - ans = max(ans, dp[i][j]); + f[i][j] = 1 + (j > 0 ? f[i - 1][j - 1] : 0); + ans = max(ans, f[i][j]); } } } @@ -149,26 +151,66 @@ public: #### Go ```go -func longestRepeatingSubstring(s string) int { +func longestRepeatingSubstring(s string) (ans int) { n := len(s) - dp := make([][]int, n) - for i := range dp { - dp[i] = make([]int, n) + f := make([][]int, n) + for i := range f { + f[i] = make([]int, n) } - ans := 0 - for i := 0; i < n; i++ { - for j := i + 1; j < n; j++ { + for i := 1; i < n; i++ { + for j := 0; j < i; j++ { if s[i] == s[j] { - if i == 0 { - dp[i][j] = 1 - } else { - dp[i][j] = dp[i-1][j-1] + 1 + if j > 0 { + f[i][j] = f[i-1][j-1] } - ans = max(ans, dp[i][j]) + f[i][j]++ + ans = max(ans, f[i][j]) } } } - return ans + return +} +``` + +#### TypeScript + +```ts +function longestRepeatingSubstring(s: string): number { + const n = s.length; + const f: number[][] = Array.from({ length: n }).map(() => Array(n).fill(0)); + let ans = 0; + for (let i = 1; i < n; ++i) { + for (let j = 0; j < i; ++j) { + if (s[i] === s[j]) { + f[i][j] = 1 + (f[i - 1][j - 1] || 0); + ans = Math.max(ans, f[i][j]); + } + } + } + return ans; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn longest_repeating_substring(s: String) -> i32 { + let n = s.len(); + let mut f = vec![vec![0; n]; n]; + let mut ans = 0; + let s = s.as_bytes(); + + for i in 1..n { + for j in 0..i { + if s[i] == s[j] { + f[i][j] = if j > 0 { f[i - 1][j - 1] + 1 } else { 1 }; + ans = ans.max(f[i][j]); + } + } + } + ans + } } ``` diff --git a/solution/1000-1099/1062.Longest Repeating Substring/README_EN.md b/solution/1000-1099/1062.Longest Repeating Substring/README_EN.md index c6f581a644da9..9355ab3aadcaf 100644 --- a/solution/1000-1099/1062.Longest Repeating Substring/README_EN.md +++ b/solution/1000-1099/1062.Longest Repeating Substring/README_EN.md @@ -62,7 +62,27 @@ tags: -### Solution 1 +### Solution 1: Dynamic Programming + +We define $f[i][j]$ to represent the length of the longest repeating substring ending with $s[i]$ and $s[j]$. Initially, $f[i][j]=0$. + +We enumerate $i$ in the range $[1, n)$ and enumerate $j$ in the range $[0, i)$. If $s[i]=s[j]$, then we have: + +$$ +f[i][j]= +\begin{cases} +f[i-1][j-1]+1, & j>0 \\ +1, & j=0 +\end{cases} +$$ + +The answer is the maximum value of all $f[i][j]$. + +The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Where $n$ is the length of the string $s$. + +Similar problems: + +- [1044. Longest Duplicate Substring 🔒](https://github.com/doocs/leetcode/blob/main/solution/1000-1099/1044.Longest%20Duplicate%20Substring/README_EN.md) @@ -72,13 +92,13 @@ tags: class Solution: def longestRepeatingSubstring(self, s: str) -> int: n = len(s) - dp = [[0] * n for _ in range(n)] + f = [[0] * n for _ in range(n)] ans = 0 - for i in range(n): - for j in range(i + 1, n): + for i in range(1, n): + for j in range(i): if s[i] == s[j]: - dp[i][j] = dp[i - 1][j - 1] + 1 if i else 1 - ans = max(ans, dp[i][j]) + f[i][j] = 1 + (f[i - 1][j - 1] if j else 0) + ans = max(ans, f[i][j]) return ans ``` @@ -88,13 +108,13 @@ class Solution: class Solution { public int longestRepeatingSubstring(String s) { int n = s.length(); + int[][] f = new int[n][n]; int ans = 0; - int[][] dp = new int[n][n]; - for (int i = 0; i < n; ++i) { - for (int j = i + 1; j < n; ++j) { + for (int i = 1; i < n; ++i) { + for (int j = 0; j < i; ++j) { if (s.charAt(i) == s.charAt(j)) { - dp[i][j] = i > 0 ? dp[i - 1][j - 1] + 1 : 1; - ans = Math.max(ans, dp[i][j]); + f[i][j] = 1 + (j > 0 ? f[i - 1][j - 1] : 0); + ans = Math.max(ans, f[i][j]); } } } @@ -109,14 +129,15 @@ class Solution { class Solution { public: int longestRepeatingSubstring(string s) { - int n = s.size(); - vector> dp(n, vector(n)); + int n = s.length(); + int f[n][n]; + memset(f, 0, sizeof(f)); int ans = 0; - for (int i = 0; i < n; ++i) { - for (int j = i + 1; j < n; ++j) { + for (int i = 1; i < n; ++i) { + for (int j = 0; j < i; ++j) { if (s[i] == s[j]) { - dp[i][j] = i ? dp[i - 1][j - 1] + 1 : 1; - ans = max(ans, dp[i][j]); + f[i][j] = 1 + (j > 0 ? f[i - 1][j - 1] : 0); + ans = max(ans, f[i][j]); } } } @@ -128,26 +149,66 @@ public: #### Go ```go -func longestRepeatingSubstring(s string) int { +func longestRepeatingSubstring(s string) (ans int) { n := len(s) - dp := make([][]int, n) - for i := range dp { - dp[i] = make([]int, n) + f := make([][]int, n) + for i := range f { + f[i] = make([]int, n) } - ans := 0 - for i := 0; i < n; i++ { - for j := i + 1; j < n; j++ { + for i := 1; i < n; i++ { + for j := 0; j < i; j++ { if s[i] == s[j] { - if i == 0 { - dp[i][j] = 1 - } else { - dp[i][j] = dp[i-1][j-1] + 1 + if j > 0 { + f[i][j] = f[i-1][j-1] } - ans = max(ans, dp[i][j]) + f[i][j]++ + ans = max(ans, f[i][j]) } } } - return ans + return +} +``` + +#### TypeScript + +```ts +function longestRepeatingSubstring(s: string): number { + const n = s.length; + const f: number[][] = Array.from({ length: n }).map(() => Array(n).fill(0)); + let ans = 0; + for (let i = 1; i < n; ++i) { + for (let j = 0; j < i; ++j) { + if (s[i] === s[j]) { + f[i][j] = 1 + (f[i - 1][j - 1] || 0); + ans = Math.max(ans, f[i][j]); + } + } + } + return ans; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn longest_repeating_substring(s: String) -> i32 { + let n = s.len(); + let mut f = vec![vec![0; n]; n]; + let mut ans = 0; + let s = s.as_bytes(); + + for i in 1..n { + for j in 0..i { + if s[i] == s[j] { + f[i][j] = if j > 0 { f[i - 1][j - 1] + 1 } else { 1 }; + ans = ans.max(f[i][j]); + } + } + } + ans + } } ``` diff --git a/solution/1000-1099/1062.Longest Repeating Substring/Solution.cpp b/solution/1000-1099/1062.Longest Repeating Substring/Solution.cpp index e8d4237e5ef1b..4e7811621fec3 100644 --- a/solution/1000-1099/1062.Longest Repeating Substring/Solution.cpp +++ b/solution/1000-1099/1062.Longest Repeating Substring/Solution.cpp @@ -1,17 +1,18 @@ class Solution { public: int longestRepeatingSubstring(string s) { - int n = s.size(); - vector> dp(n, vector(n)); + int n = s.length(); + int f[n][n]; + memset(f, 0, sizeof(f)); int ans = 0; - for (int i = 0; i < n; ++i) { - for (int j = i + 1; j < n; ++j) { + for (int i = 1; i < n; ++i) { + for (int j = 0; j < i; ++j) { if (s[i] == s[j]) { - dp[i][j] = i ? dp[i - 1][j - 1] + 1 : 1; - ans = max(ans, dp[i][j]); + f[i][j] = 1 + (j > 0 ? f[i - 1][j - 1] : 0); + ans = max(ans, f[i][j]); } } } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/1000-1099/1062.Longest Repeating Substring/Solution.go b/solution/1000-1099/1062.Longest Repeating Substring/Solution.go index d1ae5a55276d0..a4371e26d8962 100644 --- a/solution/1000-1099/1062.Longest Repeating Substring/Solution.go +++ b/solution/1000-1099/1062.Longest Repeating Substring/Solution.go @@ -1,21 +1,19 @@ -func longestRepeatingSubstring(s string) int { +func longestRepeatingSubstring(s string) (ans int) { n := len(s) - dp := make([][]int, n) - for i := range dp { - dp[i] = make([]int, n) + f := make([][]int, n) + for i := range f { + f[i] = make([]int, n) } - ans := 0 - for i := 0; i < n; i++ { - for j := i + 1; j < n; j++ { + for i := 1; i < n; i++ { + for j := 0; j < i; j++ { if s[i] == s[j] { - if i == 0 { - dp[i][j] = 1 - } else { - dp[i][j] = dp[i-1][j-1] + 1 + if j > 0 { + f[i][j] = f[i-1][j-1] } - ans = max(ans, dp[i][j]) + f[i][j]++ + ans = max(ans, f[i][j]) } } } - return ans -} \ No newline at end of file + return +} diff --git a/solution/1000-1099/1062.Longest Repeating Substring/Solution.java b/solution/1000-1099/1062.Longest Repeating Substring/Solution.java index 4f745bde390e9..fc16f454e3d52 100644 --- a/solution/1000-1099/1062.Longest Repeating Substring/Solution.java +++ b/solution/1000-1099/1062.Longest Repeating Substring/Solution.java @@ -1,16 +1,16 @@ class Solution { public int longestRepeatingSubstring(String s) { int n = s.length(); + int[][] f = new int[n][n]; int ans = 0; - int[][] dp = new int[n][n]; - for (int i = 0; i < n; ++i) { - for (int j = i + 1; j < n; ++j) { + for (int i = 1; i < n; ++i) { + for (int j = 0; j < i; ++j) { if (s.charAt(i) == s.charAt(j)) { - dp[i][j] = i > 0 ? dp[i - 1][j - 1] + 1 : 1; - ans = Math.max(ans, dp[i][j]); + f[i][j] = 1 + (j > 0 ? f[i - 1][j - 1] : 0); + ans = Math.max(ans, f[i][j]); } } } return ans; } -} \ No newline at end of file +} diff --git a/solution/1000-1099/1062.Longest Repeating Substring/Solution.py b/solution/1000-1099/1062.Longest Repeating Substring/Solution.py index a03407f1e5c2a..ee10cc0fad4f1 100644 --- a/solution/1000-1099/1062.Longest Repeating Substring/Solution.py +++ b/solution/1000-1099/1062.Longest Repeating Substring/Solution.py @@ -1,11 +1,11 @@ class Solution: def longestRepeatingSubstring(self, s: str) -> int: n = len(s) - dp = [[0] * n for _ in range(n)] + f = [[0] * n for _ in range(n)] ans = 0 - for i in range(n): - for j in range(i + 1, n): + for i in range(1, n): + for j in range(i): if s[i] == s[j]: - dp[i][j] = dp[i - 1][j - 1] + 1 if i else 1 - ans = max(ans, dp[i][j]) + f[i][j] = 1 + (f[i - 1][j - 1] if j else 0) + ans = max(ans, f[i][j]) return ans diff --git a/solution/1000-1099/1062.Longest Repeating Substring/Solution.rs b/solution/1000-1099/1062.Longest Repeating Substring/Solution.rs new file mode 100644 index 0000000000000..4ed8e53d075a4 --- /dev/null +++ b/solution/1000-1099/1062.Longest Repeating Substring/Solution.rs @@ -0,0 +1,18 @@ +impl Solution { + pub fn longest_repeating_substring(s: String) -> i32 { + let n = s.len(); + let mut f = vec![vec![0; n]; n]; + let mut ans = 0; + let s = s.as_bytes(); + + for i in 1..n { + for j in 0..i { + if s[i] == s[j] { + f[i][j] = if j > 0 { f[i - 1][j - 1] + 1 } else { 1 }; + ans = ans.max(f[i][j]); + } + } + } + ans + } +} diff --git a/solution/1000-1099/1062.Longest Repeating Substring/Solution.ts b/solution/1000-1099/1062.Longest Repeating Substring/Solution.ts new file mode 100644 index 0000000000000..42120294b543e --- /dev/null +++ b/solution/1000-1099/1062.Longest Repeating Substring/Solution.ts @@ -0,0 +1,14 @@ +function longestRepeatingSubstring(s: string): number { + const n = s.length; + const f: number[][] = Array.from({ length: n }).map(() => Array(n).fill(0)); + let ans = 0; + for (let i = 1; i < n; ++i) { + for (let j = 0; j < i; ++j) { + if (s[i] === s[j]) { + f[i][j] = 1 + (f[i - 1][j - 1] || 0); + ans = Math.max(ans, f[i][j]); + } + } + } + return ans; +}