|
| 1 | +--- |
| 2 | +title: 3375.使数组的值全部为 K 的最少操作次数:O(1)空间——排序+一次遍历 |
| 3 | +date: 2025-04-09 22:20:52 |
| 4 | +tags: [题解, LeetCode, 简单, 数组, 排序, 遍历, 模拟] |
| 5 | +categories: [题解, LeetCode] |
| 6 | +--- |
| 7 | + |
| 8 | +# 【LetMeFly】3375.使数组的值全部为 K 的最少操作次数:O(1)空间——排序+一次遍历 |
| 9 | + |
| 10 | +力扣题目链接:[https://leetcode.cn/problems/minimum-operations-to-make-array-values-equal-to-k/](https://leetcode.cn/problems/minimum-operations-to-make-array-values-equal-to-k/) |
| 11 | + |
| 12 | +<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> 。</p> |
| 13 | + |
| 14 | +<p>如果一个数组中所有 <strong>严格大于</strong> <code>h</code> 的整数值都 <strong>相等</strong> ,那么我们称整数 <code>h</code> 是 <strong>合法的</strong> 。</p> |
| 15 | + |
| 16 | +<p>比方说,如果 <code>nums = [10, 8, 10, 8]</code> ,那么 <code>h = 9</code> 是一个 <strong>合法</strong> 整数,因为所有满足 <code>nums[i] > 9</code> 的数都等于 10 ,但是 5 不是 <strong>合法</strong> 整数。</p> |
| 17 | + |
| 18 | +<p>你可以对 <code>nums</code> 执行以下操作:</p> |
| 19 | + |
| 20 | +<ul> |
| 21 | + <li>选择一个整数 <code>h</code> ,它对于 <strong>当前</strong> <code>nums</code> 中的值是合法的。</li> |
| 22 | + <li>对于每个下标 <code>i</code> ,如果它满足 <code>nums[i] > h</code> ,那么将 <code>nums[i]</code> 变为 <code>h</code> 。</li> |
| 23 | +</ul> |
| 24 | + |
| 25 | +<p>你的目标是将 <code>nums</code> 中的所有元素都变为 <code>k</code> ,请你返回 <strong>最少</strong> 操作次数。如果无法将所有元素都变 <code>k</code> ,那么返回 -1 。</p> |
| 26 | + |
| 27 | +<p> </p> |
| 28 | + |
| 29 | +<p><strong class="example">示例 1:</strong></p> |
| 30 | + |
| 31 | +<div class="example-block"> |
| 32 | +<p><span class="example-io"><b>输入:</b>nums = [5,2,5,4,5], k = 2</span></p> |
| 33 | + |
| 34 | +<p><span class="example-io"><b>输出:</b>2</span></p> |
| 35 | + |
| 36 | +<p><b>解释:</b></p> |
| 37 | + |
| 38 | +<p>依次选择合法整数 4 和 2 ,将数组全部变为 2 。</p> |
| 39 | +</div> |
| 40 | + |
| 41 | +<p><strong class="example">示例 2:</strong></p> |
| 42 | + |
| 43 | +<div class="example-block"> |
| 44 | +<p><span class="example-io"><b>输入:</b>nums = [2,1,2], k = 2</span></p> |
| 45 | + |
| 46 | +<p><span class="example-io"><b>输出:</b>-1</span></p> |
| 47 | + |
| 48 | +<p><strong>解释:</strong></p> |
| 49 | + |
| 50 | +<p>没法将所有值变为 2 。</p> |
| 51 | +</div> |
| 52 | + |
| 53 | +<p><strong class="example">示例 3:</strong></p> |
| 54 | + |
| 55 | +<div class="example-block"> |
| 56 | +<p><span class="example-io"><b>输入:</b>nums = [9,7,5,3], k = 1</span></p> |
| 57 | + |
| 58 | +<p><span class="example-io"><b>输出:</b>4</span></p> |
| 59 | + |
| 60 | +<p><strong>解释:</strong></p> |
| 61 | + |
| 62 | +<p>依次选择合法整数 7 ,5 ,3 和 1 ,将数组全部变为 1 。</p> |
| 63 | +</div> |
| 64 | + |
| 65 | +<p> </p> |
| 66 | + |
| 67 | +<p><strong>提示:</strong></p> |
| 68 | + |
| 69 | +<ul> |
| 70 | + <li><code>1 <= nums.length <= 100 </code></li> |
| 71 | + <li><code>1 <= nums[i] <= 100</code></li> |
| 72 | + <li><code>1 <= k <= 100</code></li> |
| 73 | +</ul> |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +## 解题方法:排序+遍历 |
| 78 | + |
| 79 | +这是一道阅读理解题。 |
| 80 | + |
| 81 | +先将数组从大到小排个序,若最小值小于$k$则直接返回$-1$,否则继续。 |
| 82 | + |
| 83 | +从(第二个元素开始)前到后遍历数组,若当前元素与上一个元素不同,则需要将上一个元素(和所有与之相等的元素)经过一次操作变成当前元素,操作次数加一。 |
| 84 | + |
| 85 | +最终(遍历完成后),数组中所有值都会变成$k$。如果最小值不是$k$,则还需要额外的一次变换将所有值都变成$k$。 |
| 86 | + |
| 87 | ++ 时间复杂度$O(n\log n)$,其中$n=len(nums)$ |
| 88 | ++ 空间复杂度$O(1)$ |
| 89 | + |
| 90 | +### AC代码 |
| 91 | + |
| 92 | +#### C++ |
| 93 | + |
| 94 | +```cpp |
| 95 | +/* |
| 96 | + * @Author: LetMeFly |
| 97 | + * @Date: 2025-04-09 21:58:45 |
| 98 | + * @LastEditors: LetMeFly.xyz |
| 99 | + * @LastEditTime: 2025-04-09 22:08:39 |
| 100 | + */ |
| 101 | +#if defined(_WIN32) || defined(__APPLE__) |
| 102 | +#include "_[1,2]toVector.h" |
| 103 | +#endif |
| 104 | + |
| 105 | +class Solution { |
| 106 | +public: |
| 107 | + int minOperations(vector<int>& nums, int k) { |
| 108 | + sort(nums.begin(), nums.end(), greater<int>()); |
| 109 | + if (nums.back() < k) { |
| 110 | + return -1; |
| 111 | + } |
| 112 | + int ans = 0; |
| 113 | + for (int i = 1; i < nums.size(); i++) { |
| 114 | + if (nums[i] != nums[i - 1]) { |
| 115 | + printf("nums[%d] = %d, nums[%d] = %d, ans = %d, ans++\n", i, nums[i], i - 1, nums[i - 1], ans); // **** |
| 116 | + ans++; |
| 117 | + } |
| 118 | + } |
| 119 | + return ans + (nums.back() != k); |
| 120 | + } |
| 121 | +}; |
| 122 | +``` |
| 123 | +
|
| 124 | +#### Python |
| 125 | +
|
| 126 | +```python |
| 127 | +''' |
| 128 | +Author: LetMeFly |
| 129 | +Date: 2025-04-09 22:09:48 |
| 130 | +LastEditors: LetMeFly.xyz |
| 131 | +LastEditTime: 2025-04-09 22:10:06 |
| 132 | +''' |
| 133 | +from typing import List |
| 134 | +
|
| 135 | +class Solution: |
| 136 | + def minOperations(self, nums: List[int], k: int) -> int: |
| 137 | + nums.sort(reverse=True) |
| 138 | + if nums[-1] < k: |
| 139 | + return -1 |
| 140 | + ans = 0 |
| 141 | + for i in range(1, len(nums)): |
| 142 | + if nums[i] != nums[i - 1]: |
| 143 | + ans += 1 |
| 144 | + return ans + (nums[-1] != k) |
| 145 | +``` |
| 146 | + |
| 147 | +#### Java |
| 148 | + |
| 149 | +```java |
| 150 | +/* |
| 151 | + * @Author: LetMeFly |
| 152 | + * @Date: 2025-04-09 22:12:36 |
| 153 | + * @LastEditors: LetMeFly.xyz |
| 154 | + * @LastEditTime: 2025-04-09 22:15:42 |
| 155 | + */ |
| 156 | +import java.util.Arrays; |
| 157 | + |
| 158 | +class Solution { |
| 159 | + public int minOperations(int[] nums, int k) { |
| 160 | + Arrays.sort(nums); |
| 161 | + if (nums[0] < k) { |
| 162 | + return -1; |
| 163 | + } |
| 164 | + int ans = 0; |
| 165 | + for (int i = nums.length - 1; i > 0; i--) { |
| 166 | + if (nums[i] != nums[i - 1]) { |
| 167 | + ans++; |
| 168 | + } |
| 169 | + } |
| 170 | + if (nums[0] != k) { |
| 171 | + ans++; |
| 172 | + } |
| 173 | + return ans; |
| 174 | + } |
| 175 | +} |
| 176 | +``` |
| 177 | + |
| 178 | +#### Go |
| 179 | + |
| 180 | +```go |
| 181 | +/* |
| 182 | + * @Author: LetMeFly |
| 183 | + * @Date: 2025-04-09 22:16:49 |
| 184 | + * @LastEditors: LetMeFly.xyz |
| 185 | + * @LastEditTime: 2025-04-09 22:18:14 |
| 186 | + */ |
| 187 | +package main |
| 188 | + |
| 189 | +import "slices" |
| 190 | + |
| 191 | +func minOperations(nums []int, k int) (ans int) { |
| 192 | + slices.Sort(nums) |
| 193 | + if nums[0] < k { |
| 194 | + return -1 |
| 195 | + } |
| 196 | + for i := len(nums) - 1; i > 0; i-- { |
| 197 | + if nums[i] != nums[i - 1] { |
| 198 | + ans++ |
| 199 | + } |
| 200 | + } |
| 201 | + if nums[0] != k { |
| 202 | + ans++ |
| 203 | + } |
| 204 | + return |
| 205 | +} |
| 206 | +``` |
| 207 | + |
| 208 | +> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/147104288)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/04/09/LeetCode%203375.%E4%BD%BF%E6%95%B0%E7%BB%84%E7%9A%84%E5%80%BC%E5%85%A8%E9%83%A8%E4%B8%BAK%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0/)哦~ |
| 209 | +> |
| 210 | +> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode) |
0 commit comments