← Home

808. Soup Servings

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
dynamic programmingC++Markdown
808

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 808. Soup Servings, the solution in this repository is mainly a dynamic programming solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. 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: dynamic programming.

The notes already sitting in the source point us in the right direction:

  • 20 / 41 test cases passed.

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 soupServings, prob, f.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • 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:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

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

solution.cpp
01//Runtime Error: stack-overflow on address
02//20 / 41 test cases passed.
03class Solution {
04public:
05    vector<int> serveAs;
06    unordered_map<string, vector<double>> memo;
07    
08    vector<double> soupServings(int NA, int NB){
09        /*
10        prob[0]: A will be empty first
11        prob[1]: A and B become empty at the same time
12        */
13        string key = to_string(NA) + "_" + to_string(NB);
14        if(memo.find(key) != memo.end()){
15            return memo[key];
16        }
17        
18        vector<double> prob(2);
19        
20        for(int serveA : serveAs){
21            int serveB = 100 - serveA;
22            if(NA <= serveA && NB <= serveB){
23                prob[1] += 0.25;
24            }else if(NA <= serveA){
25                prob[0] += 0.25;
26            }else if(NB <= serveB){
27                //pass
28            }else{
29                vector<double> subprob = soupServings(NA-serveA, NB-serveB);
30                prob[0] += 0.25 * subprob[0];
31                prob[1] += 0.25 * subprob[1];
32            }
33        }
34        
35        return memo[key] = prob;
36    };
37    
38    double soupServings(int N) {
39        serveAs = {100, 75, 50, 25};
40        vector<double> prob = soupServings(N, N);
41        return prob[0] + prob[1] * 0.5;
42    }
43};
44
45//return 1 when N >= 4800
46/*
47Answers within 10^-5 of the true value will be accepted as correct.
48When N = 4800, the result = 0.999994994426
49*/
50//https://leetcode.com/problems/soup-servings/discuss/121711/C%2B%2BJavaPython-When-N-greater-4800-just-return-1
51//Runtime: 32 ms, faster than 15.42% of C++ online submissions for Soup Servings.
52//Memory Usage: 8.9 MB, less than 41.48% of C++ online submissions for Soup Servings.
53class Solution {
54public:
55    vector<int> serveAs;
56    unordered_map<string, vector<double>> memo;
57    
58    vector<double> soupServings(int NA, int NB){
59        /*
60        prob[0]: A will be empty first
61        prob[1]: A and B become empty at the same time
62        */
63        if(NA >= 4800) return {1.0, 0.0};
64        string key = to_string(NA) + "_" + to_string(NB);
65        if(memo.find(key) != memo.end()){
66            return memo[key];
67        }
68        
69        vector<double> prob(2);
70        
71        for(int serveA : serveAs){
72            int serveB = 100 - serveA;
73            if(NA <= serveA && NB <= serveB){
74                prob[1] += 0.25;
75            }else if(NA <= serveA){
76                prob[0] += 0.25;
77            }else if(NB <= serveB){
78                //pass
79            }else{
80                vector<double> subprob = soupServings(NA-serveA, NB-serveB);
81                prob[0] += 0.25 * subprob[0];
82                prob[1] += 0.25 * subprob[1];
83            }
84        }
85        
86        return memo[key] = prob;
87    };
88    
89    double soupServings(int N) {
90        serveAs = {100, 75, 50, 25};
91        vector<double> prob = soupServings(N, N);
92        return prob[0] + prob[1] * 0.5;
93    }
94};
95
96//conversion
97//Runtime: 8 ms, faster than 52.57% of C++ online submissions for Soup Servings.
98//Memory Usage: 14.5 MB, less than 13.63% of C++ online submissions for Soup Servings.
99class Solution {
100public:
101    vector<vector<double>> memo;
102    
103    double f(int NA, int NB){
104        if(NA <= 0 && NB <= 0) return 0.5;
105        if(NA <= 0) return 1.0;
106        if(NB <= 0) return 0.0;
107        if(memo[NA][NB] > 0) return memo[NA][NB];
108        return memo[NA][NB] = 0.25*(f(NA-4,NB)+f(NA-3,NB-1)+f(NA-2,NB-2)+f(NA-1,NB-3));
109    };
110    
111    double soupServings(int N) {
112        if(N > 4800) return 1.0;
113        //200 because 4800/25+1=193, it's around 200
114        memo = vector<vector<double>>(200, vector<double>(200));
115        //conversion from 25's multiple to the number of spoons
116        return f(ceil((double)N/25), ceil((double)N/25));
117    }
118};
119
120//conversion, using array
121//https://leetcode.com/problems/soup-servings/discuss/121711/C%2B%2BJavaPython-When-N-greater-4800-just-return-1
122//Runtime: 4 ms, faster than 69.96% of C++ online submissions for Soup Servings.
123//Memory Usage: 6.2 MB, less than 95.45% of C++ online submissions for Soup Servings.
124class Solution {
125public:
126    double memo[200][200];
127    
128    double f(int NA, int NB){
129        if(NA <= 0 && NB <= 0) return 0.5;
130        if(NA <= 0) return 1.0;
131        if(NB <= 0) return 0.0;
132        if(memo[NA][NB] > 0) return memo[NA][NB];
133        return memo[NA][NB] = 0.25*(f(NA-4,NB)+f(NA-3,NB-1)+f(NA-2,NB-2)+f(NA-1,NB-3));
134    };
135    
136    double soupServings(int N) {
137        if(N > 4800) return 1.0;
138        //conversion from 25's multiple to the number of spoons
139        return f(ceil((double)N/25), ceil((double)N/25));
140    }
141};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.