I like to read this solution as a small machine: keep the useful information, throw away the noise. For 453. Minimum Moves to Equal Array Elements, the solution in this repository is mainly a greedy solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. 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:
- Time Limit Exceeded
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are minMoves, ones.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- 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: Time Limit Exceeded
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Time Limit Exceeded
02class Solution {
03public:
04 int minMoves(vector<int>& nums) {
05 int count = 0, n = nums.size();
06 vector<int> ones(n, 0);
07
08 int cur_max = *max_element(nums.begin(), nums.end());
09
10 while(adjacent_find(nums.begin(), nums.end(), not_equal_to<>()) != nums.end()){
11 sort(nums.begin(), nums.end());
12 // std::copy(nums.begin(), nums.end(),
13 // std::ostream_iterator<int>(std::cout, " "));
14 // std::cout << std::endl;
15
16 // int second_largest = nums[n-1];
17 // for(int i = n-2; i >= 0; i--){
18 // if(second_largest != nums[i]){
19 // second_largest = nums[i];
20 // break;
21 // }
22 // }
23
24 // int diff = nums[n-1] - second_largest;
25 long diff = nums[n-1] - nums[0];
26 // long diff = *max_element(nums.begin(), nums.end()) - *min_element(nums.begin(), nums.end());
27 fill(ones.begin(), ones.end()-1, diff);
28 transform(nums.begin(), nums.end(),
29 ones.begin(), nums.begin(),
30 plus<long>());
31 count += diff;
32 }
33
34 return count;
35 }
36};
37
38//https://leetcode.com/problems/minimum-moves-to-equal-array-elements/discuss/93817/It-is-a-math-question
39//Runtime: 48 ms, faster than 93.11% of C++ online submissions for Minimum Moves to Equal Array Elements.
40//Memory Usage: 11 MB, less than 77.78% of C++ online submissions for Minimum Moves to Equal Array Elements.
41class Solution {
42public:
43 int minMoves(vector<int>& nums) {
44 long long sum = accumulate(nums.begin(), nums.end(), 0LL);
45 int minimum = *min_element(nums.begin(), nums.end());
46 int n = nums.size();
47
48 return sum - (long long)minimum * n;
49 }
50};
Cost