The problem is taken from LeetCode (#808): “Soup Servings”.
There are two types of soup: type A and type B. Initially, we have N
ml of soup. There are four kinds of operations:
When we serve some soup, we consume the specified number of milliliters from soup A and soup B. After soup has been served, we consider the following events later:
Return the probability that soup A will be empty first, plus half the probability that A and B become empty at the same time.
N
is very large?N
for which the precision should be accounted?Assumption: For large values of N
, the result is very close to 1.
import java.util.HashMap;
import java.util.Map;
public class SoupServings {
private Map<String, Double> memo = new HashMap<>();
public double soupServings(int N) {
if (N > 4800)
return 1.0;
return serve(N, N);
}
private double serve(int A, int B) {
if (A <= 0 && B <= 0)
return 0.5;
if (A <= 0)
return 1.0;
if (B <= 0)
return 0.0;
String key = A + "," + B;
if (memo.containsKey(key))
return memo.get(key);
double probability = 0.25 * (
serve(A - 100, B) +
serve(A - 75, B - 25) +
serve(A - 50, B - 50) +
serve(A - 25, B - 75)
);
memo.put(key, probability);
return probability;
}
public static void main(String[] args) {
SoupServings solution = new SoupServings();
System.out.println(solution.soupServings(50)); // Example input
System.out.println(solution.soupServings(100)); // Example input
}
}
serve
function uses a HashMap
to memorize previously computed values to avoid redundant calculations and retain the intermediate results.A
and B
are exhausted simultaneously, return 0.5
.A
is exhausted, return 1.0
.B
is exhausted, return 0.0
.0.25
.N
optimization: For large values (greater than 4800), the result is approximated to 1.0
due to diminishing probability differences.The time complexity is O(N^2) due to memoization, where N
is the soup quantity in the worst case, noting that N
in this context will be reduced by certain thresholds, making the number of effective calls intractable. This is efficient given memoized results are reused.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?