A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 1558. Minimum Numbers of Function Calls to Make Target Array, the solution in this repository is mainly a greedy solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. 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:
- Greedy
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 minOperations.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- 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:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
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//Greedy
02//Runtime: 256 ms, faster than 40.00% of C++ online submissions for Minimum Numbers of Function Calls to Make Target Array.
03//Memory Usage: 25.7 MB, less than 20.00% of C++ online submissions for Minimum Numbers of Function Calls to Make Target Array.
04class Solution {
05public:
06 int minOperations(vector<int>& nums) {
07 int ans = 0;
08
09 while(any_of(nums.begin(), nums.end(), [](const int& e){return e >= 2;})){
10 for(int& num : nums){
11 if(num&1){
12 ++ans;
13 --num;
14 }
15 }
16
17 transform(nums.begin(), nums.end(), nums.begin(), [](int& x){return x>>1;});
18 ++ans;
19 }
20
21 ans += accumulate(nums.begin(), nums.end(), 0);
22
23 return ans;
24 }
25};
26
27//Bit counts, greedy
28//https://leetcode.com/problems/minimum-numbers-of-function-calls-to-make-target-array/discuss/805740/JavaC%2B%2BPython-Bit-Counts
29//Runtime: 180 ms, faster than 40.00% of C++ online submissions for Minimum Numbers of Function Calls to Make Target Array.
30//Memory Usage: 25.4 MB, less than 100.00% of C++ online submissions for Minimum Numbers of Function Calls to Make Target Array.
31class Solution {
32public:
33 int minOperations(vector<int>& nums) {
34 int maxbits = 0;
35 int op1 = 0; //the count of operation 1(add by 1)
36
37 for(int& num : nums){
38 int bits = 0;
39
40 // cout << num << " ";
41
42 while(num){
43 ++bits;
44 maxbits = max(maxbits, bits);
45 op1 += num&1;
46 num >>= 1;
47 }
48
49 // cout << bits << " " << op1 << endl;
50 }
51
52 /*
53 operation 2(multiply by 2) is shared,
54 the count of operation 2 needed is the
55 max length of nums's numbers' bit representation-1
56
57 for example, 5 is 101:
58 101 -> 100 -> 10 -> 1 -> 0
59 and we need 2 operation two
60 */
61 return op1 + maxbits-1;
62 }
63};
Cost