Let's make this one less mysterious. For 1509. Minimum Difference Between Largest and Smallest Value in Three Moves, the solution in this repository is mainly a greedy solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: greedy.
The notes already sitting in the source point us in the right direction:
- sort
- https://www.geeksforgeeks.org/minimize-the-maximum-minimum-difference-after-one-removal-from-array/?ref=rp
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are minDifference.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//sort
02//https://www.geeksforgeeks.org/minimize-the-maximum-minimum-difference-after-one-removal-from-array/?ref=rp
03//Runtime: 324 ms, faster than 33.33% of C++ online submissions for Minimum Difference Between Largest and Smallest Value in Three Moves.
04//Memory Usage: 35.4 MB, less than 100.00% of C++ online submissions for Minimum Difference Between Largest and Smallest Value in Three Moves.
05class Solution {
06public:
07 int minDifference(vector<int>& nums) {
08 int n = nums.size();
09
10 // cout << "n: " << n << endl;
11
12 if(n <= 3) return 0;
13
14 sort(nums.begin(), nums.end());
15
16 int minDiff = INT_MAX;
17
18 /*
19 From the problem:
20 Return the minimum difference between the largest and smallest value of nums after perfoming at most 3 moves.
21 that means we can remove 3 elements,
22 and following are the 4 ways of removing 3 elements
23 */
24 for(int i = 0; i <= 3; ++i){
25 minDiff = min(minDiff, nums[n-1-i] - nums[3-i]);
26 }
27
28 // return min(nums[n-4] - nums[0], nums[n-1] - nums[3]);
29 return minDiff;
30 }
31};
32
33//partial_sort
34//https://leetcode.com/problems/minimum-difference-between-largest-and-smallest-value-in-three-moves/discuss/730567/JavaC%2B%2BPython-Straight-Forward
35//Runtime: 188 ms, faster than 100.00% of C++ online submissions for Minimum Difference Between Largest and Smallest Value in Three Moves.
36//Memory Usage: 35.4 MB, less than 100.00% of C++ online submissions for Minimum Difference Between Largest and Smallest Value in Three Moves.
37class Solution {
38public:
39 int minDifference(vector<int>& nums) {
40 int n = nums.size();
41
42 // cout << "n: " << n << endl;
43
44 //after removing 3 elements, there will be at most one element
45 if(n <= 4) return 0;
46
47 /*
48 looking at [nums.begin(), nums.end()),
49 and make sure [nums.begin(), nums.begin()+4) is sorted
50 */
51 partial_sort(nums.begin(), nums.begin()+4, nums.end());
52 /*
53 looking at [nums.begin()+4, nums.end()),
54 and make sure nth position has the right number,
55 also elements before nth element are all <= nth element,
56 and elements afater nth element are all >= nth element
57 */
58 nth_element(nums.begin()+4, nums.end()-4, nums.end());
59 //now [nums.end()-4, nums.end()] are in right position
60
61 //sort [nums.end()-4, nums.end()]
62 sort(nums.end()-4, nums.end());
63
64 int minDiff = INT_MAX;
65
66 for(int i = 0; i <= 3; ++i){
67 minDiff = min(minDiff, nums[n-1-i] - nums[3-i]);
68 }
69
70 return minDiff;
71 }
72};
Cost