Skip to content

feat: add solutions to lc problem: No.3375 #4339

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

Merged
merged 1 commit into from
Apr 8, 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 @@ -88,7 +88,13 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:哈希表

根据题目描述,我们每次可以选择当前数组中的次大值作为合法整数 $h$,将所有大于 $h$ 的数都变为 $h$,这样可以使得操作次数最少。另外,由于操作会使得数字变小,因此,如果当前数组中存在小于 $k$ 的数,那么我们就无法将所有数都变为 $k$,直接返回 -1 即可。

我们遍历数组 $\textit{nums}$,对于当前的数 $x$,如果 $x < k$,直接返回 -1;否则,我们将 $x$ 加入哈希表中,并且更新当前数组中的最小值 $\textit{mi}$。最后,我们返回哈希表的大小减去 1(如果 $\textit{mi} = k$,则需要减去 1)。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -183,6 +189,29 @@ function minOperations(nums: number[], k: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn min_operations(nums: Vec<i32>, k: i32) -> i32 {
use std::collections::HashSet;

let mut s = HashSet::new();
let mut mi = i32::MAX;

for &x in &nums {
if x < k {
return -1;
}
s.insert(x);
mi = mi.min(x);
}

(s.len() as i32) - if mi == k { 1 } else { 0 }
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Hash Table

According to the problem description, we can choose the second largest value in the current array as the valid integer $h$ each time, and change all numbers greater than $h$ to $h$. This minimizes the number of operations. Additionally, since the operation reduces the numbers, if there are numbers in the current array smaller than $k$, we cannot make all numbers equal to $k$, so we directly return -1.

We iterate through the array $\textit{nums}$. For the current number $x$, if $x < k$, we directly return -1. Otherwise, we add $x$ to the hash table and update the minimum value $\textit{mi}$ in the current array. Finally, we return the size of the hash table minus 1 (if $\textit{mi} = k$, we need to subtract 1).

Time complexity is $O(n)$, and space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$.

<!-- tabs:start -->

Expand Down Expand Up @@ -181,6 +187,29 @@ function minOperations(nums: number[], k: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn min_operations(nums: Vec<i32>, k: i32) -> i32 {
use std::collections::HashSet;

let mut s = HashSet::new();
let mut mi = i32::MAX;

for &x in &nums {
if x < k {
return -1;
}
s.insert(x);
mi = mi.min(x);
}

(s.len() as i32) - if mi == k { 1 } else { 0 }
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
impl Solution {
pub fn min_operations(nums: Vec<i32>, k: i32) -> i32 {
use std::collections::HashSet;

let mut s = HashSet::new();
let mut mi = i32::MAX;

for &x in &nums {
if x < k {
return -1;
}
s.insert(x);
mi = mi.min(x);
}

(s.len() as i32) - if mi == k { 1 } else { 0 }
}
}