Skip to content

Commit 20281bd

Browse files
authored
Merge pull request #861 from LetMeFly666/3375
添加问题“3375.使数组的值全部为 K 的最少操作次数”的代码和题解
2 parents ad987b8 + 62e66ae commit 20281bd

6 files changed

+307
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-09 21:58:45
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-09 22:08:39
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int minOperations(vector<int>& nums, int k) {
14+
sort(nums.begin(), nums.end(), greater<int>());
15+
if (nums.back() < k) {
16+
return -1;
17+
}
18+
int ans = 0;
19+
for (int i = 1; i < nums.size(); i++) {
20+
if (nums[i] != nums[i - 1]) {
21+
printf("nums[%d] = %d, nums[%d] = %d, ans = %d, ans++\n", i, nums[i], i - 1, nums[i - 1], ans); // ****
22+
ans++;
23+
}
24+
}
25+
return ans + (nums.back() != k);
26+
}
27+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-09 22:16:49
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-09 22:18:14
6+
*/
7+
package main
8+
9+
import "slices"
10+
11+
func minOperations(nums []int, k int) (ans int) {
12+
slices.Sort(nums)
13+
if nums[0] < k {
14+
return -1
15+
}
16+
for i := len(nums) - 1; i > 0; i-- {
17+
if nums[i] != nums[i - 1] {
18+
ans++
19+
}
20+
}
21+
if nums[0] != k {
22+
ans++
23+
}
24+
return
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-04-09 22:12:36
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-04-09 22:15:42
6+
*/
7+
import java.util.Arrays;
8+
9+
class Solution {
10+
public int minOperations(int[] nums, int k) {
11+
Arrays.sort(nums);
12+
if (nums[0] < k) {
13+
return -1;
14+
}
15+
int ans = 0;
16+
for (int i = nums.length - 1; i > 0; i--) {
17+
if (nums[i] != nums[i - 1]) {
18+
ans++;
19+
}
20+
}
21+
if (nums[0] != k) {
22+
ans++;
23+
}
24+
return ans;
25+
}
26+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-04-09 22:09:48
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-04-09 22:10:06
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def minOperations(self, nums: List[int], k: int) -> int:
11+
nums.sort(reverse=True)
12+
if nums[-1] < k:
13+
return -1
14+
ans = 0
15+
for i in range(1, len(nums)):
16+
if nums[i] != nums[i - 1]:
17+
ans += 1
18+
return ans + (nums[-1] != k)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,7 @@
942942
|3305.元音辅音字符串计数I|中等|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/12/LeetCode%203305.%E5%85%83%E9%9F%B3%E8%BE%85%E9%9F%B3%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%AE%A1%E6%95%B0I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146197747" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-i/solutions/3607571/letmefly-3305yuan-yin-fu-yin-zi-fu-chuan-ptkg/" target="_blank">LeetCode题解</a>|
943943
|3306.元音辅音字符串计数II|中等|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/13/LeetCode%203306.%E5%85%83%E9%9F%B3%E8%BE%85%E9%9F%B3%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%AE%A1%E6%95%B0II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146228751" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/solutions/3609545/letmefly-3306yuan-yin-fu-yin-zi-fu-chuan-7y2q/" target="_blank">LeetCode题解</a>|
944944
|3340.检查平衡字符串|简单|<a href="https://leetcode.cn/problems/check-balanced-string/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/14/LeetCode%203340.%E6%A3%80%E6%9F%A5%E5%B9%B3%E8%A1%A1%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146249653" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-balanced-string/solutions/3610899/letmefly-3340jian-cha-ping-heng-zi-fu-ch-p8eo/" target="_blank">LeetCode题解</a>|
945+
|3375.使数组的值全部为K的最少操作次数|简单|<a href="https://leetcode.cn/problems/minimum-operations-to-make-array-values-equal-to-k/" target="_blank">题目地址</a>|<a href="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/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147104288" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-operations-to-make-array-values-equal-to-k/solutions/3646138/letmefly-3375shi-shu-zu-de-zhi-quan-bu-w-i71e/" target="_blank">LeetCode题解</a>|
945946
|3396.使数组元素互不相同所需的最少操作次数|简单|<a href="https://leetcode.cn/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/04/08/LeetCode%203396.%E4%BD%BF%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E4%BA%92%E4%B8%8D%E7%9B%B8%E5%90%8C%E6%89%80%E9%9C%80%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/147080178" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-of-operations-to-make-elements-in-array-distinct/solutions/3644951/letmefly-3396shi-shu-zu-yuan-su-hu-bu-xi-glg4/" target="_blank">LeetCode题解</a>|
946947
|剑指Offer0047.礼物的最大价值|简单|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/08/LeetCode%20%E5%89%91%E6%8C%87%20Offer%2047.%20%E7%A4%BC%E7%89%A9%E7%9A%84%E6%9C%80%E5%A4%A7%E4%BB%B7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129408765" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/solutions/2155672/letmefly-jian-zhi-offer-47li-wu-de-zui-d-rekb/" target="_blank">LeetCode题解</a>|
947948
|剑指OfferII0041.滑动窗口的平均值|简单|<a href="https://leetcode.cn/problems/qIsx9U/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/16/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200041.%20%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E7%9A%84%E5%B9%B3%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125819216" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/qIsx9U/solution/by-tisfy-30mq/" target="_blank">LeetCode题解</a>|
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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>给你一个整数数组&nbsp;<code>nums</code>&nbsp;和一个整数&nbsp;<code>k</code>&nbsp;。</p>
13+
14+
<p>如果一个数组中所有 <strong>严格大于</strong>&nbsp;<code>h</code>&nbsp;的整数值都 <strong>相等</strong>&nbsp;,那么我们称整数&nbsp;<code>h</code>&nbsp;是 <strong>合法的</strong>&nbsp;。</p>
15+
16+
<p>比方说,如果&nbsp;<code>nums = [10, 8, 10, 8]</code>&nbsp;,那么&nbsp;<code>h = 9</code>&nbsp;是一个 <strong>合法</strong>&nbsp;整数,因为所有满足&nbsp;<code>nums[i] &gt; 9</code>&nbsp;的数都等于 10 ,但是 5 不是 <strong>合法</strong>&nbsp;整数。</p>
17+
18+
<p>你可以对 <code>nums</code>&nbsp;执行以下操作:</p>
19+
20+
<ul>
21+
<li>选择一个整数&nbsp;<code>h</code>&nbsp;,它对于 <strong>当前</strong>&nbsp;<code>nums</code>&nbsp;中的值是合法的。</li>
22+
<li>对于每个下标 <code>i</code>&nbsp;,如果它满足&nbsp;<code>nums[i] &gt; h</code>&nbsp;,那么将&nbsp;<code>nums[i]</code>&nbsp;变为&nbsp;<code>h</code>&nbsp;。</li>
23+
</ul>
24+
25+
<p>你的目标是将 <code>nums</code>&nbsp;中的所有元素都变为 <code>k</code>&nbsp;,请你返回 <strong>最少</strong>&nbsp;操作次数。如果无法将所有元素都变&nbsp;<code>k</code>&nbsp;,那么返回 -1 。</p>
26+
27+
<p>&nbsp;</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>&nbsp;</p>
66+
67+
<p><strong>提示:</strong></p>
68+
69+
<ul>
70+
<li><code>1 &lt;= nums.length &lt;= 100 </code></li>
71+
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
72+
<li><code>1 &lt;= k &lt;= 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

Comments
 (0)