808. Soup Servings LeetCode Solution
In this guide, you will get 808. Soup Servings LeetCode Solution with the best time and space complexity. The solution to Soup Servings problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Soup Servings solution in C++
- Soup Servings solution in Java
- Soup Servings solution in Python
- Additional Resources

Problem Statement of Soup Servings
There are two types of soup: type A and type B. Initially, we have n ml of each type of soup. There are four kinds of operations:
Serve 100 ml of soup A and 0 ml of soup B,
Serve 75 ml of soup A and 25 ml of soup B,
Serve 50 ml of soup A and 50 ml of soup B, and
Serve 25 ml of soup A and 75 ml of soup B.
When we serve some soup, we give it to someone, and we no longer have it. Each turn, we will choose from the four operations with an equal probability 0.25. If the remaining volume of soup is not enough to complete the operation, we will serve as much as possible. We stop once we no longer have some quantity of both types of soup.
Note that we do not have an operation where all 100 ml’s of soup B are used first.
Return the probability that soup A will be empty first, plus half the probability that A and B become empty at the same time. Answers within 10-5 of the actual answer will be accepted.
Example 1:
Input: n = 50
Output: 0.62500
Explanation: If we choose the first two operations, A will become empty first.
For the third operation, A and B will become empty at the same time.
For the fourth operation, B will become empty first.
So the total probability of A becoming empty first plus half the probability that A and B become empty at the same time, is 0.25 * (1 + 1 + 0.5 + 0) = 0.625.
Example 2:
Input: n = 100
Output: 0.71875
Constraints:
0 <= n <= 109
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(1)
808. Soup Servings LeetCode Solution in C++
class Solution {
public:
double soupServings(int n) {
return n >= 4800 ? 1.0 : dfs((n + 24) / 25, (n + 24) / 25);
}
private:
vector<vector<double>> mem =
vector<vector<double>>(4800 / 25, vector<double>(4800 / 25));
double dfs(int a, int b) {
if (a <= 0 && b <= 0)
return 0.5;
if (a <= 0)
return 1.0;
if (b <= 0)
return 0.0;
if (mem[a][b] > 0)
return mem[a][b];
return mem[a][b] = 0.25 * (dfs(a - 4, b) + dfs(a - 3, b - 1) +
dfs(a - 2, b - 2) + dfs(a - 1, b - 3));
}
};
/* code provided by PROGIEZ */
808. Soup Servings LeetCode Solution in Java
class Solution {
public double soupServings(int n) {
return n >= 4800 ? 1.0 : dfs((n + 24) / 25, (n + 24) / 25);
}
private double[][] mem = new double[4800 / 25][4800 / 25];
private double dfs(int a, int b) {
if (a <= 0 && b <= 0)
return 0.5;
if (a <= 0)
return 1.0;
if (b <= 0)
return 0.0;
if (mem[a][b] > 0)
return mem[a][b];
return mem[a][b] =
.25 * (dfs(a - 4, b) + dfs(a - 3, b - 1) + dfs(a - 2, b - 2) + dfs(a - 1, b - 3));
}
}
// code provided by PROGIEZ
808. Soup Servings LeetCode Solution in Python
class Solution:
def soupServings(self, n: int) -> float:
@functools.lru_cache(None)
def dfs(a: int, b: int) -> float:
if a <= 0 and b <= 0:
return 0.5
if a <= 0:
return 1.0
if b <= 0:
return 0.0
return 0.25 * (dfs(a - 4, b) +
dfs(a - 3, b - 1) +
dfs(a - 2, b - 2) +
dfs(a - 1, b - 3))
return 1 if n >= 4800 else dfs((n + 24) // 25, (n + 24) // 25)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.