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: add solutions to lc problem: No.2829 #4156

Merged
merged 1 commit into from
Mar 13, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ tags:
<strong>输入:</strong>n = 2, k = 6
<strong>输出:</strong>3
<strong>解释:</strong>可以构造数组 [1,2] ,其元素总和为 3 。
可以证明不存在总和小于 3 的 k-avoiding 数组。
可以证明不存在总和小于 3 的 k-avoiding 数组。
</pre>

<p>&nbsp;</p>
Expand All @@ -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$ 为数组长度。

<!-- tabs:start -->

Expand All @@ -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
```

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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
}
Expand All @@ -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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- tabs:start -->

Expand All @@ -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
```

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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
}
Expand All @@ -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
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
};
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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;
}