-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1509. Minimum Difference Between Largest and Smallest Value in Three Moves.cpp
82 lines (62 loc) · 2.6 KB
/
1509. Minimum Difference Between Largest and Smallest Value in Three Moves.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
You are given an integer array nums.
In one move, you can choose one element of nums and change it to any value.
Return the minimum difference between the largest and smallest value of nums after performing at most three moves.
Example 1:
Input: nums = [5,3,2,4]
Output: 0
Explanation: We can make at most 3 moves.
In the first move, change 2 to 3. nums becomes [5,3,3,4].
In the second move, change 4 to 3. nums becomes [5,3,3,3].
In the third move, change 5 to 3. nums becomes [3,3,3,3].
After performing 3 moves, the difference between the minimum and maximum is 3 - 3 = 0.
Example 2:
Input: nums = [1,5,0,10,14]
Output: 1
Explanation: We can make at most 3 moves.
In the first move, change 5 to 0. nums becomes [1,0,0,10,14].
In the second move, change 10 to 0. nums becomes [1,0,0,0,14].
In the third move, change 14 to 1. nums becomes [1,0,0,0,1].
After performing 3 moves, the difference between the minimum and maximum is 1 - 0 = 1.
It can be shown that there is no way to make the difference 0 in 3 moves.
Example 3:
Input: nums = [3,100,20]
Output: 0
Explanation: We can make at most 3 moves.
In the first move, change 100 to 7. nums becomes [3,7,20].
In the second move, change 20 to 7. nums becomes [3,7,7].
In the third move, change 3 to 7. nums becomes [7,7,7].
After performing 3 moves, the difference between the minimum and maximum is 7 - 7 = 0.
Constraints:
1 <= nums.length <= 105
-109 <= nums[i] <= 109
*/
class Solution {
public:
int minDifference(vector<int>& nums) {
int numsSize = nums.size();
// If the array has 4 or fewer elements, return 0
if (numsSize <= 4) return 0;
sort(nums.begin(), nums.end());
int minDiff = INT_MAX;
// Four scenarios to compute the minimum difference
for (int left = 0, right = numsSize - 4; left < 4; left++, right++) {
minDiff = min(minDiff, nums[right] - nums[left]);
}
return minDiff;
}
};
/*
Algorithm
Initialization:
Determine the size of the array nums and store it in numsSize.
If numsSize is less than or equal to 4, return 0.
Sort the array nums.
Initialize minDiff to a very large number to store the minimum difference.
Iterate through the first four elements of the sorted array:
For each index left from 0 to 3:
Calculate the corresponding right index as numsSize - 4 + left.
Compute the difference between the elements at indices right and left in the sorted array.
Update minDiff with the minimum value between minDiff and the computed difference.
Return minDiff, which stores the minimum difference between the largest and smallest values after removing up to three elements
*/