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. 三次操作后最大值与最小值的最小差 - 官方题解^]
0 commit comments