|
| 1 | +# 1509. Minimum Difference Between Largest and Smallest Value in Three Moves |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Array, Greedy, Sorting. |
| 5 | +- Similar Questions: Minimize the Maximum Difference of Pairs. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +You are given an integer array `nums`. |
| 10 | + |
| 11 | +In one move, you can choose one element of `nums` and change it to **any value**. |
| 12 | + |
| 13 | +Return **the minimum difference between the largest and smallest value of `nums` **after performing at most three moves****. |
| 14 | + |
| 15 | + |
| 16 | +Example 1: |
| 17 | + |
| 18 | +``` |
| 19 | +Input: nums = [5,3,2,4] |
| 20 | +Output: 0 |
| 21 | +Explanation: We can make at most 3 moves. |
| 22 | +In the first move, change 2 to 3. nums becomes [5,3,3,4]. |
| 23 | +In the second move, change 4 to 3. nums becomes [5,3,3,3]. |
| 24 | +In the third move, change 5 to 3. nums becomes [3,3,3,3]. |
| 25 | +After performing 3 moves, the difference between the minimum and maximum is 3 - 3 = 0. |
| 26 | +``` |
| 27 | + |
| 28 | +Example 2: |
| 29 | + |
| 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: |
| 42 | + |
| 43 | +``` |
| 44 | +Input: nums = [3,100,20] |
| 45 | +Output: 0 |
| 46 | +Explanation: We can make at most 3 moves. |
| 47 | +In the first move, change 100 to 7. nums becomes [3,7,20]. |
| 48 | +In the second move, change 20 to 7. nums becomes [3,7,7]. |
| 49 | +In the third move, change 3 to 7. nums becomes [7,7,7]. |
| 50 | +After performing 3 moves, the difference between the minimum and maximum is 7 - 7 = 0. |
| 51 | +``` |
| 52 | + |
| 53 | + |
| 54 | +**Constraints:** |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +- `1 <= nums.length <= 105` |
| 59 | + |
| 60 | +- `-109 <= nums[i] <= 109` |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +## Solution |
| 65 | + |
| 66 | +```javascript |
| 67 | +/** |
| 68 | + * @param {number[]} nums |
| 69 | + * @return {number} |
| 70 | + */ |
| 71 | +var minDifference = function(nums) { |
| 72 | + if (nums.length <= 4) return 0; |
| 73 | + nums.sort((a, b) => a - b); |
| 74 | + return Math.min( |
| 75 | + nums[nums.length - 4] - nums[0], |
| 76 | + nums[nums.length - 3] - nums[1], |
| 77 | + nums[nums.length - 2] - nums[2], |
| 78 | + nums[nums.length - 1] - nums[3], |
| 79 | + ); |
| 80 | +}; |
| 81 | +``` |
| 82 | + |
| 83 | +**Explain:** |
| 84 | + |
| 85 | +nope. |
| 86 | + |
| 87 | +**Complexity:** |
| 88 | + |
| 89 | +* Time complexity : O(n * log(n)). |
| 90 | +* Space complexity : O(n). |
| 91 | + |
| 92 | +## Solution 2 |
| 93 | + |
| 94 | +```javascript |
| 95 | +/** |
| 96 | + * @param {number[]} nums |
| 97 | + * @return {number} |
| 98 | + */ |
| 99 | +var minDifference = function(nums) { |
| 100 | + if (nums.length <= 4) return 0; |
| 101 | + var minQueue = new MinPriorityQueue(); |
| 102 | + var maxQueue = new MaxPriorityQueue(); |
| 103 | + for (var i = 0; i < nums.length; i++) { |
| 104 | + minQueue.enqueue(nums[i], nums[i]); |
| 105 | + if (minQueue.size() > 4) { |
| 106 | + minQueue.dequeue(); |
| 107 | + } |
| 108 | + maxQueue.enqueue(nums[i], nums[i]); |
| 109 | + if (maxQueue.size() > 4) { |
| 110 | + maxQueue.dequeue(); |
| 111 | + } |
| 112 | + } |
| 113 | + const arr = [ |
| 114 | + ...maxQueue.toArray().map(item => item.element).reverse(), |
| 115 | + ...minQueue.toArray().map(item => item.element), |
| 116 | + ]; |
| 117 | + return Math.min( |
| 118 | + arr[arr.length - 4] - arr[0], |
| 119 | + arr[arr.length - 3] - arr[1], |
| 120 | + arr[arr.length - 2] - arr[2], |
| 121 | + arr[arr.length - 1] - arr[3], |
| 122 | + ); |
| 123 | +}; |
| 124 | +``` |
| 125 | + |
| 126 | +**Explain:** |
| 127 | + |
| 128 | +nope. |
| 129 | + |
| 130 | +**Complexity:** |
| 131 | + |
| 132 | +* Time complexity : O(n * log(4) * log(4)) = O(n). |
| 133 | +* Space complexity : O(1). |
0 commit comments