diff --git a/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README.md b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README.md index 9111c5bcd1067..f1e73283c5c07 100644 --- a/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README.md +++ b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README.md @@ -88,7 +88,13 @@ tags: -### 方法一 +### 方法一:哈希表 + +根据题目描述,我们每次可以选择当前数组中的次大值作为合法整数 $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}$ 的长度。 @@ -183,6 +189,29 @@ function minOperations(nums: number[], k: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn min_operations(nums: Vec, 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 } + } +} +``` + diff --git a/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README_EN.md b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README_EN.md index d780280fbeb07..2dd2f6dd1d718 100644 --- a/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README_EN.md +++ b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/README_EN.md @@ -86,7 +86,13 @@ tags: -### 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}$. @@ -181,6 +187,29 @@ function minOperations(nums: number[], k: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn min_operations(nums: Vec, 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 } + } +} +``` + diff --git a/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/Solution.rs b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/Solution.rs new file mode 100644 index 0000000000000..93b5ea2f478ef --- /dev/null +++ b/solution/3300-3399/3375.Minimum Operations to Make Array Values Equal to K/Solution.rs @@ -0,0 +1,18 @@ +impl Solution { + pub fn min_operations(nums: Vec, 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 } + } +}