-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathletcode-q-2.java
More file actions
36 lines (29 loc) · 1.06 KB
/
letcode-q-2.java
File metadata and controls
36 lines (29 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
private Double[][] cache; // Memoization storage
public double soupServings(int n) {
// Large n behaves like probability 1
if (n > 5000) return 1.0;
int units = (int) Math.ceil(n / 25.0); // Convert mL → 25 mL units
cache = new Double[units + 1][units + 1];
return calcProb(units, units);
}
private double calcProb(int soupA, int soupB) {
// Both soups empty → half probability
if (soupA <= 0 && soupB <= 0) return 0.5;
// A empty first
if (soupA <= 0) return 1.0;
// B empty first
if (soupB <= 0) return 0.0;
// If already computed, return cached result
if (cache[soupA][soupB] != null) return cache[soupA][soupB];
// Calculate and store probability
double prob = 0.25 * (
calcProb(soupA - 4, soupB) +
calcProb(soupA - 3, soupB - 1) +
calcProb(soupA - 2, soupB - 2) +
calcProb(soupA - 1, soupB - 3)
);
cache[soupA][soupB] = prob;
return prob;
}
}