Given an array nums
, you are allowed to choose one element of nums
and change it by any value in one move.
Return the minimum difference between the largest and smallest value of nums
after perfoming at most 3 moves.
Example 1:
Input: nums = [5,3,2,4] Output: 0 Explanation: Change the array [5,3,2,4] to [2,2,2,2]. The difference between the maximum and minimum is 2-2 = 0.
Example 2:
Input: nums = [1,5,0,10,14] Output: 1 Explanation: Change the array [1,5,0,10,14] to [1,1,0,1,1]. The difference between the maximum and minimum is 1-0 = 1.
Example 3:
Input: nums = [6,6,0,1,1,4,6] Output: 2
Example 4:
Input: nums = [1,5,6,14,15] Output: 1
Constraints:
1 <= nums.length <= 10^5
-10^9 <= nums[i] <= 10^9
三次操作后最大值與最小值的最小差。
給你一個數組 nums ,每次操作你可以選擇 nums 中的任意一個元素並將它改成任意值。
請你返回三次操作后, nums 中最大值與最小值的差的最小值。
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/minimum-difference-between-largest-and-smallest-value-in-three-moves
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。
這道題可以排序做,也可以不排序做,但是不排序的代碼太過麻煩,排序的做法實現起來會簡單很多。一道類似的題目是1423題。
題意要求的是在給定的 input 數組里去掉三個數字,使得剩下的數字的最大值 - 最小值的差值最小。這道題的 corner case 是如果數組長度小於 5,那么基本就不需要比較了。如果數組長度大於 5,我們才需要找到最大的 4 個數字和最小的 4 個數字。所以直覺是需要排序,然后比較到底是去掉
- 最小的三個數字
- 最大的三個數字
- 最小的一個 + 最大的兩個
- 最小的兩個 + 最大的一個
時間O(nlogn)
空間O(1)
Java實現
1 class Solution { 2 public int minDifference(int[] nums) { 3 int len = nums.length; 4 // corner case 5 if (len <= 4) { 6 return 0; 7 } 8 9 // normal case 10 Arrays.sort(nums); 11 int res = Integer.MAX_VALUE; 12 for (int i = 0; i < 4; i++) { 13 res = Math.min(res, nums[len - 4 + i] - nums[i]); 14 } 15 return res; 16 } 17 }
我這里提供一個討論區看到的不排序的做法,但是為了找到最大的4個數字和最小的4個數字,這個做法還是需要利用到堆來篩選,再加上元素在堆操作的進出,其實最后代碼跑下來的速度還不如直接排序來得快。
時間O(nlogn)
空間O(n)
Java實現
1 class Solution { 2 public int minDifference(int[] nums) { 3 // corner case 4 if (nums.length < 5) { 5 return 0; 6 } 7 if (nums.length <= 8) { 8 return helper(nums, true); 9 } 10 PriorityQueue<Integer> top4 = new PriorityQueue<>(); 11 PriorityQueue<Integer> bottom4 = new PriorityQueue<>((a, b) -> b - a); 12 for (int n : nums) { 13 top4.offer(n); 14 bottom4.offer(n); 15 if (top4.size() > 4) { 16 top4.poll(); 17 } 18 if (bottom4.size() > 4) { 19 bottom4.poll(); 20 } 21 } 22 int[] arr = new int[8]; 23 for (int l = 3, r = 4; l >= 0 && r < 8; l--, r++) { 24 arr[l] = bottom4.poll(); 25 arr[r] = top4.poll(); 26 } 27 return helper(arr, false); 28 } 29 30 private int helper(int[] nums, boolean needSort) { 31 if (needSort) { 32 Arrays.sort(nums); 33 } 34 int res = Integer.MAX_VALUE; 35 int n = nums.length; 36 for (int i = 0; i < 4; i++) { 37 res = Math.min(res, nums[n - (4 - i)] - nums[i]); 38 } 39 return res; 40 } 41 }
相關題目
1423. Maximum Points You Can Obtain from Cards
1509. Minimum Difference Between Largest and Smallest Value in Three Moves