Skip to content

Commit da829cb

Browse files
committed
一刷1509
1 parent e3485e8 commit da829cb

File tree

5 files changed

+89
-56
lines changed

5 files changed

+89
-56
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10589,12 +10589,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
1058910589
//|Medium
1059010590
//|
1059110591

10592-
//|{counter:codes}
10593-
//|{leetcode_base_url}/minimum-difference-between-largest-and-smallest-value-in-three-moves/[1509. Minimum Difference Between Largest and Smallest Value in Three Moves^]
10594-
//|{source_base_url}/_1509_MinimumDifferenceBetweenLargestAndSmallestValueInThreeMoves.java[Java]
10595-
//|{doc_base_url}/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves.adoc[题解]
10596-
//|Medium
10597-
//|
10592+
|{counter:codes}
10593+
|{leetcode_base_url}/minimum-difference-between-largest-and-smallest-value-in-three-moves/[1509. Minimum Difference Between Largest and Smallest Value in Three Moves^]
10594+
|{source_base_url}/_1509_MinimumDifferenceBetweenLargestAndSmallestValueInThreeMoves.java[Java]
10595+
|{doc_base_url}/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves.adoc[题解]
10596+
|Medium
10597+
|
1059810598

1059910599
//|{counter:codes}
1060010600
//|{leetcode_base_url}/stone-game-iv/[1510. Stone Game IV^]
Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,63 @@
11
[#1509-minimum-difference-between-largest-and-smallest-value-in-three-moves]
2-
= 1509. Minimum Difference Between Largest and Smallest Value in Three Moves
2+
= 1509. 三次操作后最大值与最小值的最小差
33

4-
{leetcode}/problems/minimum-difference-between-largest-and-smallest-value-in-three-moves/[LeetCode - 1509. Minimum Difference Between Largest and Smallest Value in Three Moves ^]
4+
https://leetcode.cn/problems/minimum-difference-between-largest-and-smallest-value-in-three-moves/[LeetCode - 1509. 三次操作后最大值与最小值的最小差 ^]
55

6-
You are given an integer array `nums`.
6+
给你一个数组 `nums`
77

8-
In one move, you can choose one element of `nums` and change it to *any value*.
8+
每次操作你可以选择 `nums` 中的任意一个元素并将它改成 *任意值*
99

10-
Return _the minimum difference between the largest and smallest value of `nums` *after performing at most three moves*_.
10+
*执行最多三次移动后 *,返回 `nums` 中最大值与最小值的最小差值。
1111
12-
13-
*Example 1:*
12+
*示例 1:*
1413

15-
[subs="verbatim,quotes"]
16-
----
17-
*Input:* nums = [5,3,2,4]
18-
*Output:* 0
19-
*Explanation:* We can make at most 3 moves.
20-
In the first move, change 2 to 3. nums becomes [5,3,3,4].
21-
In the second move, change 4 to 3. nums becomes [5,3,3,3].
22-
In the third move, change 5 to 3. nums becomes [3,3,3,3].
23-
After performing 3 moves, the difference between the minimum and maximum is 3 - 3 = 0.
24-
25-
----
26-
27-
*Example 2:*
28-
29-
[subs="verbatim,quotes"]
30-
----
31-
*Input:* nums = [1,5,0,10,14]
32-
*Output:* 1
33-
*Explanation:* We can make at most 3 moves.
34-
In the first move, change 5 to 0. nums becomes [1,0,0,10,14].
35-
In the second move, change 10 to 0. nums becomes [1,0,0,0,14].
36-
In the third move, change 14 to 1. nums becomes [1,0,0,0,1].
37-
After performing 3 moves, the difference between the minimum and maximum is 1 - 0 = 1.
38-
It can be shown that there is no way to make the difference 0 in 3 moves.
39-
----
40-
41-
*Example 3:*
14+
....
15+
输入:nums = [5,3,2,4]
16+
输出:0
17+
解释:我们最多可以走 3 步。
18+
第一步,将 2 变为 3 。 nums 变成 [5,3,3,4] 。
19+
第二步,将 4 改为 3 。 nums 变成 [5,3,3,3] 。
20+
第三步,将 5 改为 3 。 nums 变成 [3,3,3,3] 。
21+
执行 3 次移动后,最小值和最大值之间的差值为 3 - 3 = 0 。
22+
....
4223

43-
[subs="verbatim,quotes"]
44-
----
45-
*Input:* nums = [3,100,20]
46-
*Output:* 0
47-
*Explanation:* We can make at most 3 moves.
48-
In the first move, change 100 to 7. nums becomes [3,7,20].
49-
In the second move, change 20 to 7. nums becomes [3,7,7].
50-
In the third move, change 3 to 7. nums becomes [7,7,7].
51-
After performing 3 moves, the difference between the minimum and maximum is 7 - 7 = 0.
24+
*示例 2:*
5225

53-
----
26+
....
27+
输入:nums = [1,5,0,10,14]
28+
输出:1
29+
解释:我们最多可以走 3 步。
30+
第一步,将 5 改为 0 。 nums变成 [1,0,0,10,14] 。
31+
第二步,将 10 改为 0 。 nums变成 [1,0,0,0,14] 。
32+
第三步,将 14 改为 1 。 nums变成 [1,0,0,0,1] 。
33+
执行 3 步后,最小值和最大值之间的差值为 1 - 0 = 1 。
34+
可以看出,没有办法可以在 3 步内使差值变为0。
35+
....
5436

55-
56-
*Constraints:*
37+
*示例 3:*
5738

39+
....
40+
输入:nums = [3,100,20]
41+
输出:0
42+
解释:我们最多可以走 3 步。
43+
第一步,将 100 改为 7 。 nums 变成 [3,7,20] 。
44+
第二步,将 20 改为 7 。 nums 变成 [3,7,7] 。
45+
第三步,将 3 改为 7 。 nums 变成 [7,7,7] 。
46+
执行 3 步后,最小值和最大值之间的差值是 7 - 7 = 0。
47+
....
5848

59-
* `1 <= nums.length <= 10^5^`
60-
* `-10^9^ <= nums[i] <= 10^9^`
6149

50+
*提示:*
6251

52+
* `1 \<= nums.length \<= 10^5^`
53+
* `-10^9^ \<= nums[i] \<= 10^9^`
6354
6455
6556
== 思路分析
6657

58+
先对数组进行排序,要求最大值与最小值的最小差值,那么就是把减少最大值和最小值直接的差值,要么把最大值变小,要么把最小值变大,或者两者都要。所以,就要看那种方式能对差值改变最大。
59+
60+
看官方题解,可以直接求 stem:[a(n - 4 + i) - a(i)] 的最小值。
6761

6862
[[src-1509]]
6963
[tabs]
@@ -90,4 +84,4 @@ include::{sourcedir}/_1509_MinimumDifferenceBetweenLargestAndSmallestValueInThre
9084

9185
== 参考资料
9286

93-
87+
. https://leetcode.cn/problems/minimum-difference-between-largest-and-smallest-value-in-three-moves/solutions/336428/san-ci-cao-zuo-hou-zui-da-zhi-yu-zui-xiao-zhi-de-2/[1509. 三次操作后最大值与最小值的最小差 - 官方题解^]

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3101,7 +3101,7 @@ include::1480-running-sum-of-1d-array.adoc[leveloffset=+1]
31013101

31023102
// include::1508-range-sum-of-sorted-subarray-sums.adoc[leveloffset=+1]
31033103

3104-
// include::1509-minimum-difference-between-largest-and-smallest-value-in-three-moves.adoc[leveloffset=+1]
3104+
include::1509-minimum-difference-between-largest-and-smallest-value-in-three-moves.adoc[leveloffset=+1]
31053105

31063106
// include::1510-stone-game-iv.adoc[leveloffset=+1]
31073107

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,11 @@ endif::[]
763763
|{doc_base_url}/3111-minimum-rectangles-to-cover-points.adoc[题解]
764764
|✅ 贪心。从左边界开始,尽可能多占用横轴。宽度耗尽,则从下一个点开始。
765765

766+
|{counter:codes}
767+
|{leetcode_base_url}/minimum-difference-between-largest-and-smallest-value-in-three-moves/[1509. 三次操作后最大值与最小值的最小差^]
768+
|{doc_base_url}/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves.adoc[题解]
769+
|✅ 贪心。转变一下题目就是要求如何缩短最大值和最小值直接的差值。看官方题解,可以直接求 stem:[a(n - 4 + i) - a(i)] 的最小值。
770+
766771
|===
767772

768773
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.Arrays;
4+
5+
public class _1509_MinimumDifferenceBetweenLargestAndSmallestValueInThreeMoves {
6+
// tag::answer[]
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2025-05-21 22:54:47
10+
*/
11+
public int minDifference(int[] nums) {
12+
int length = nums.length;
13+
if (length <= 4) {
14+
return 0;
15+
}
16+
Arrays.sort(nums);
17+
int diff = nums[length - 1] - nums[0];
18+
int max = 0;
19+
// max = Math.max(max, (nums[3] - nums[0]) + (nums[length - 1] - nums[length - 1]));
20+
// max = Math.max(max, (nums[2] - nums[0]) + (nums[length - 1] - nums[length - 2]));
21+
// max = Math.max(max, (nums[1] - nums[0]) + (nums[length - 1] - nums[length - 3]));
22+
// max = Math.max(max, (nums[0] - nums[0]) + (nums[length - 1] - nums[length - 4]));
23+
for (int i = 3; i >= 0; i--) {
24+
max = Math.max(max, (nums[i] - nums[0]) + (nums[length - 1] - nums[length - (4 - i)]));
25+
}
26+
return diff - max;
27+
}
28+
// end::answer[]
29+
public static void main(String[] args) {
30+
new _1509_MinimumDifferenceBetweenLargestAndSmallestValueInThreeMoves()
31+
.minDifference(new int[]{1, 5, 0, 10, 14});
32+
}
33+
34+
}

0 commit comments

Comments
 (0)