|
| 1 | +--- |
| 2 | +title: 3375. 使数组的值全部为 K 的最少操作次数 |
| 3 | +description: LeetCode 3375. 使数组的值全部为 K 的最少操作次数题解,Minimum Operations to Make Array Values Equal to K,包含解题思路、复杂度分析以及完整的 JavaScript 代码实现。 |
| 4 | +keywords: |
| 5 | + - LeetCode |
| 6 | + - 3375. 使数组的值全部为 K 的最少操作次数 |
| 7 | + - 使数组的值全部为 K 的最少操作次数 |
| 8 | + - Minimum Operations to Make Array Values Equal to K |
| 9 | + - 解题思路 |
| 10 | + - 数组 |
| 11 | + - 哈希表 |
| 12 | +--- |
| 13 | + |
| 14 | +# 3375. 使数组的值全部为 K 的最少操作次数 |
| 15 | + |
| 16 | +🟢 <font color=#15bd66>Easy</font>  🔖  [`数组`](/tag/array.md) [`哈希表`](/tag/hash-table.md)  🔗 [`力扣`](https://leetcode.cn/problems/minimum-operations-to-make-array-values-equal-to-k) [`LeetCode`](https://leetcode.com/problems/minimum-operations-to-make-array-values-equal-to-k) |
| 17 | + |
| 18 | +## 题目 |
| 19 | + |
| 20 | +You are given an integer array `nums` and an integer `k`. |
| 21 | + |
| 22 | +An integer `h` is called **valid** if all values in the array that are |
| 23 | +**strictly greater** than `h` are _identical_. |
| 24 | + |
| 25 | +For example, if `nums = [10, 8, 10, 8]`, a **valid** integer is `h = 9` |
| 26 | +because all `nums[i] > 9` are equal to 10, but 5 is not a **valid** integer. |
| 27 | + |
| 28 | +You are allowed to perform the following operation on `nums`: |
| 29 | + |
| 30 | +- Select an integer `h` that is _valid_ for the **current** values in `nums`. |
| 31 | +- For each index `i` where `nums[i] > h`, set `nums[i]` to `h`. |
| 32 | + |
| 33 | +Return the **minimum** number of operations required to make every element in |
| 34 | +`nums` **equal** to `k`. If it is impossible to make all elements equal to |
| 35 | +`k`, return -1. |
| 36 | + |
| 37 | +**Example 1:** |
| 38 | + |
| 39 | +**Input:** nums = [5,2,5,4,5], k = 2 |
| 40 | + |
| 41 | +**Output:** 2 |
| 42 | + |
| 43 | +**Explanation:** |
| 44 | + |
| 45 | +The operations can be performed in order using valid integers 4 and then 2. |
| 46 | + |
| 47 | +**Example 2:** |
| 48 | + |
| 49 | +**Input:** nums = [2,1,2], k = 2 |
| 50 | + |
| 51 | +**Output:** -1 |
| 52 | + |
| 53 | +**Explanation:** |
| 54 | + |
| 55 | +It is impossible to make all the values equal to 2. |
| 56 | + |
| 57 | +**Example 3:** |
| 58 | + |
| 59 | +**Input:** nums = [9,7,5,3], k = 1 |
| 60 | + |
| 61 | +**Output:** 4 |
| 62 | + |
| 63 | +**Explanation:** |
| 64 | + |
| 65 | +The operations can be performed using valid integers in the order 7, 5, 3, and |
| 66 | + |
| 67 | +1. |
| 68 | + |
| 69 | +**Constraints:** |
| 70 | + |
| 71 | +- `1 <= nums.length <= 100 ` |
| 72 | +- `1 <= nums[i] <= 100` |
| 73 | +- `1 <= k <= 100` |
| 74 | + |
| 75 | +## 题目大意 |
| 76 | + |
| 77 | +给你一个整数数组 `nums` 和一个整数 `k` 。 |
| 78 | + |
| 79 | +如果一个数组中所有 **严格大于** `h` 的整数值都 **相等** ,那么我们称整数 `h` 是 **合法的** 。 |
| 80 | + |
| 81 | +比方说,如果 `nums = [10, 8, 10, 8]` ,那么 `h = 9` 是一个 **合法** 整数,因为所有满足 `nums[i] > 9` |
| 82 | +的数都等于 10 ,但是 5 不是 **合法** 整数。 |
| 83 | + |
| 84 | +你可以对 `nums` 执行以下操作: |
| 85 | + |
| 86 | +- 选择一个整数 `h` ,它对于 **当前** `nums` 中的值是合法的。 |
| 87 | +- 对于每个下标 `i` ,如果它满足 `nums[i] > h` ,那么将 `nums[i]` 变为 `h` 。 |
| 88 | + |
| 89 | +你的目标是将 `nums` 中的所有元素都变为 `k` ,请你返回 **最少** 操作次数。如果无法将所有元素都变 `k` ,那么返回 -1 。 |
| 90 | + |
| 91 | +**示例 1:** |
| 92 | + |
| 93 | +**输入:** nums = [5,2,5,4,5], k = 2 |
| 94 | + |
| 95 | +**输出:** 2 |
| 96 | + |
| 97 | +**解释:** |
| 98 | + |
| 99 | +依次选择合法整数 4 和 2 ,将数组全部变为 2 。 |
| 100 | + |
| 101 | +**示例 2:** |
| 102 | + |
| 103 | +**输入:** nums = [2,1,2], k = 2 |
| 104 | + |
| 105 | +**输出:** -1 |
| 106 | + |
| 107 | +**解释:** |
| 108 | + |
| 109 | +没法将所有值变为 2 。 |
| 110 | + |
| 111 | +**示例 3:** |
| 112 | + |
| 113 | +**输入:** nums = [9,7,5,3], k = 1 |
| 114 | + |
| 115 | +**输出:** 4 |
| 116 | + |
| 117 | +**解释:** |
| 118 | + |
| 119 | +依次选择合法整数 7 ,5 ,3 和 1 ,将数组全部变为 1 。 |
| 120 | + |
| 121 | +**提示:** |
| 122 | + |
| 123 | +- `1 <= nums.length <= 100 ` |
| 124 | +- `1 <= nums[i] <= 100` |
| 125 | +- `1 <= k <= 100` |
| 126 | + |
| 127 | +## 解题思路 |
| 128 | + |
| 129 | +- 初始化一个空的 `Set` 用于存储所有大于 `k` 的不同元素。 |
| 130 | +- 遍历数组: |
| 131 | + - 如果遇到小于 `k` 的元素,直接返回 `-1`。 |
| 132 | + - 如果等于 `k`,跳过。 |
| 133 | + - 如果大于 `k`,加入 `Set`。 |
| 134 | +- 最后返回 `Set` 的大小,即为不同的大于 `k` 的元素数量。 |
| 135 | + |
| 136 | +#### 复杂度分析 |
| 137 | + |
| 138 | +- **时间复杂度**:`O(n)`,其中 `n` 是数组 `nums` 的长度,遍历一遍数组。 |
| 139 | +- **空间复杂度**:`O(n)`,最坏情况下所有元素都大于 `k` 且互不相同,`Set` 最多存储 `n` 个元素。 |
| 140 | + |
| 141 | +## 代码 |
| 142 | + |
| 143 | +```javascript |
| 144 | +/** |
| 145 | + * @param {number[]} nums |
| 146 | + * @param {number} k |
| 147 | + * @return {number} |
| 148 | + */ |
| 149 | +var minOperations = function (nums, k) { |
| 150 | + let unique = new Set(); |
| 151 | + for (let num of nums) { |
| 152 | + if (num < k) return -1; |
| 153 | + if (num == k) continue; |
| 154 | + unique.add(num); |
| 155 | + } |
| 156 | + return unique.size; |
| 157 | +}; |
| 158 | +``` |
0 commit comments