From 076a5c3d448620b493fc37b875a9f67e45f25159 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 13 Mar 2025 12:51:15 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2829 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; }