I like to read this solution as a small machine: keep the useful information, throw away the noise. For 1402. Reducing Dishes, 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.
Guide
When?
This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are maxSatisfaction.
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: O(NlogN), space: O(1)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 12 ms, faster than 42.86% of C++ online submissions for Reducing Dishes.
02//Memory Usage: 6.4 MB, less than 100.00% of C++ online submissions for Reducing Dishes.
03class Solution {
04public:
05 int maxSatisfaction(vector<int>& satisfaction) {
06 bool allPos = true, allNeg = true;
07 for(int s : satisfaction){
08 if(s > 0) allNeg = false;
09 if(s < 0) allPos = false;
10 if(!allPos && !allNeg) break;
11 }
12 if(allNeg) return 0;
13
14 int ltc = 0;
15
16 sort(satisfaction.begin(), satisfaction.end());
17 for(int i = 0; i < satisfaction.size(); i++){
18 // cout << i << " " << satisfaction[i] << endl;
19 ltc += (i+1) * satisfaction[i];
20 }
21
22 if(allPos){
23 return ltc;
24 }
25
26 // cout << "ltc: " << ltc << endl;
27
28 //part neg, part pos
29 int ans = 0;
30 int sum = accumulate(satisfaction.begin(), satisfaction.end(), 0);
31 for(int i = 0; i < satisfaction.size(); i++){
32 if(i > 0){
33 //update ltc
34 ltc -= sum;
35 sum -= satisfaction[i-1];
36 }
37 // cout << ltc << " " << sum << endl;
38 ans = max(ans, ltc);
39 }
40
41 return ans;
42 }
43};
44
45//clean
46//https://leetcode.com/problems/reducing-dishes/discuss/563384/JavaC%2B%2BPython-Easy-and-Concise
47//Runtime: 8 ms, faster than 42.86% of C++ online submissions for Reducing Dishes.
48//Memory Usage: 6.5 MB, less than 100.00% of C++ online submissions for Reducing Dishes.
49//time: O(NlogN), space: O(1)
50class Solution {
51public:
52 int maxSatisfaction(vector<int>& satisfaction) {
53 //descending order
54 sort(satisfaction.rbegin(), satisfaction.rend());
55
56 int ans = 0, sum = 0, ltc = 0;
57
58 /*
59 because we will add (original sum + satisfaction[i]) to ans,
60 we don't want it to be negative,
61 so we jump out if satisfaction[i] + sum <= 0
62 */
63 for(int i = 0; i < satisfaction.size() && satisfaction[i] + sum > 0; i++){
64 //cumulative sum : satisfaction[0...i]
65 sum += satisfaction[i];
66 //we add (original sum + satisfaction[i]) to ans
67 //[0...i-1]'s cooking time is increased by 1
68 //, and newly add satisfaction[i]
69 ans += sum;
70 }
71
72 return ans;
73 }
74};
Cost