Skip to content

Commit c90ca0e

Browse files
authored
feat: add solutions to lc problem: No.3375 (#4339)
No.3375.Minimum Operations to Make Array Values Equal to K
1 parent 01b8a10 commit c90ca0e

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ tags:
8888

8989
<!-- solution:start -->
9090

91-
### 方法一
91+
### 方法一:哈希表
92+
93+
根据题目描述,我们每次可以选择当前数组中的次大值作为合法整数 $h$,将所有大于 $h$ 的数都变为 $h$,这样可以使得操作次数最少。另外,由于操作会使得数字变小,因此,如果当前数组中存在小于 $k$ 的数,那么我们就无法将所有数都变为 $k$,直接返回 -1 即可。
94+
95+
我们遍历数组 $\textit{nums}$,对于当前的数 $x$,如果 $x < k$,直接返回 -1;否则,我们将 $x$ 加入哈希表中,并且更新当前数组中的最小值 $\textit{mi}$。最后,我们返回哈希表的大小减去 1(如果 $\textit{mi} = k$,则需要减去 1)。
96+
97+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。
9298

9399
<!-- tabs:start -->
94100

@@ -183,6 +189,29 @@ function minOperations(nums: number[], k: number): number {
183189
}
184190
```
185191

192+
#### Rust
193+
194+
```rust
195+
impl Solution {
196+
pub fn min_operations(nums: Vec<i32>, k: i32) -> i32 {
197+
use std::collections::HashSet;
198+
199+
let mut s = HashSet::new();
200+
let mut mi = i32::MAX;
201+
202+
for &x in &nums {
203+
if x < k {
204+
return -1;
205+
}
206+
s.insert(x);
207+
mi = mi.min(x);
208+
}
209+
210+
(s.len() as i32) - if mi == k { 1 } else { 0 }
211+
}
212+
}
213+
```
214+
186215
<!-- tabs:end -->
187216

188217
<!-- solution:end -->

solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README_EN.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,13 @@ tags:
8686

8787
<!-- solution:start -->
8888

89-
### Solution 1
89+
### Solution 1: Hash Table
90+
91+
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.
92+
93+
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).
94+
95+
Time complexity is $O(n)$, and space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$.
9096

9197
<!-- tabs:start -->
9298

@@ -181,6 +187,29 @@ function minOperations(nums: number[], k: number): number {
181187
}
182188
```
183189

190+
#### Rust
191+
192+
```rust
193+
impl Solution {
194+
pub fn min_operations(nums: Vec<i32>, k: i32) -> i32 {
195+
use std::collections::HashSet;
196+
197+
let mut s = HashSet::new();
198+
let mut mi = i32::MAX;
199+
200+
for &x in &nums {
201+
if x < k {
202+
return -1;
203+
}
204+
s.insert(x);
205+
mi = mi.min(x);
206+
}
207+
208+
(s.len() as i32) - if mi == k { 1 } else { 0 }
209+
}
210+
}
211+
```
212+
184213
<!-- tabs:end -->
185214

186215
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
impl Solution {
2+
pub fn min_operations(nums: Vec<i32>, k: i32) -> i32 {
3+
use std::collections::HashSet;
4+
5+
let mut s = HashSet::new();
6+
let mut mi = i32::MAX;
7+
8+
for &x in &nums {
9+
if x < k {
10+
return -1;
11+
}
12+
s.insert(x);
13+
mi = mi.min(x);
14+
}
15+
16+
(s.len() as i32) - if mi == k { 1 } else { 0 }
17+
}
18+
}

0 commit comments

Comments
 (0)