|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +-* 808. Soup Servings *- |
| 5 | +
|
| 6 | +
|
| 7 | +
|
| 8 | +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: |
| 9 | +
|
| 10 | +Serve 100 ml of soup A and 0 ml of soup B, |
| 11 | +Serve 75 ml of soup A and 25 ml of soup B, |
| 12 | +Serve 50 ml of soup A and 50 ml of soup B, and |
| 13 | +Serve 25 ml of soup A and 75 ml of soup B. |
| 14 | +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. |
| 15 | +
|
| 16 | +Note that we do not have an operation where all 100 ml's of soup B are used first. |
| 17 | +
|
| 18 | +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. |
| 19 | +
|
| 20 | + |
| 21 | +
|
| 22 | +Example 1: |
| 23 | +
|
| 24 | +Input: n = 50 |
| 25 | +Output: 0.62500 |
| 26 | +Explanation: If we choose the first two operations, A will become empty first. |
| 27 | +For the third operation, A and B will become empty at the same time. |
| 28 | +For the fourth operation, B will become empty first. |
| 29 | +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. |
| 30 | +Example 2: |
| 31 | +
|
| 32 | +Input: n = 100 |
| 33 | +Output: 0.71875 |
| 34 | + |
| 35 | +
|
| 36 | +Constraints: |
| 37 | +
|
| 38 | +0 <= n <= 109 |
| 39 | +
|
| 40 | +*/ |
| 41 | + |
| 42 | +import 'dart:collection'; |
| 43 | + |
| 44 | +// Recursion is BAD in Dart |
| 45 | + |
| 46 | +class A { |
| 47 | + double soupServings(int n) { |
| 48 | + double ans = solve(n, n); |
| 49 | + return ans; |
| 50 | + } |
| 51 | + |
| 52 | + double solve(int soupA, int soupB) { |
| 53 | + if (soupA <= 0 && soupB <= 0) return 0.5; |
| 54 | + if (soupA <= 0) return 1.0; |
| 55 | + if (soupB <= 0) return 0.0; |
| 56 | + |
| 57 | + double prob = 0.0; |
| 58 | + prob += 0.25 * solve(soupA - 100, soupB); |
| 59 | + prob += 0.25 * solve(soupA - 75, soupB - 25); |
| 60 | + prob += 0.25 * solve(soupA - 50, soupB - 50); |
| 61 | + prob += 0.25 * solve(soupA - 25, soupB - 75); |
| 62 | + |
| 63 | + return prob; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// Memoization - Dynamic Programming |
| 68 | +class Solution { |
| 69 | + double soupServings(int n) { |
| 70 | + if (n >= 5000) return 1.0; |
| 71 | + final HashMap<String, double> dp = HashMap(); |
| 72 | + double ans = solve(n, n, dp); |
| 73 | + return ans; |
| 74 | + } |
| 75 | + |
| 76 | + double solve( |
| 77 | + final int soupA, final int soupB, final HashMap<String, double> dp) { |
| 78 | + if (soupA <= 0 && soupB <= 0) return 0.5; |
| 79 | + if (soupA <= 0) return 1.0; |
| 80 | + if (soupB <= 0) return 0.0; |
| 81 | + |
| 82 | + String key = soupA.toString() + "-" + soupB.toString(); |
| 83 | + if (dp.containsKey(key)) return dp[key] ?? 0; |
| 84 | + |
| 85 | + double prob = 0.0; |
| 86 | + prob += 0.25 * solve(soupA - 100, soupB, dp); |
| 87 | + prob += 0.25 * solve(soupA - 75, soupB - 25, dp); |
| 88 | + prob += 0.25 * solve(soupA - 50, soupB - 50, dp); |
| 89 | + prob += 0.25 * solve(soupA - 25, soupB - 75, dp); |
| 90 | + |
| 91 | + dp[key] = prob; |
| 92 | + return prob; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// Depth First Search |
| 97 | + |
| 98 | +class B { |
| 99 | + final List<List<double>> dp = |
| 100 | + List.generate(200, (_) => List.filled(200, 0.0)); |
| 101 | + |
| 102 | + double depthFirstSearch(int soupA, int soupB) { |
| 103 | + if (soupA <= 0 && soupB <= 0) return 0.5; |
| 104 | + if (soupA <= 0) return 1.0; |
| 105 | + if (soupB <= 0) return 0.0; |
| 106 | + if (dp[soupA][soupB] > 0) return dp[soupA][soupB]; |
| 107 | + return dp[soupA][soupB] = (depthFirstSearch(soupA - 4, soupB) + |
| 108 | + depthFirstSearch(soupA - 3, soupB - 1) + |
| 109 | + depthFirstSearch(soupA - 2, soupB - 2) + |
| 110 | + depthFirstSearch(soupA - 1, soupB - 3)) / |
| 111 | + 4; |
| 112 | + } |
| 113 | + |
| 114 | + double soupServings(int n) { |
| 115 | + if (n > 4800.0) return 1.0; |
| 116 | + n = (n + 24.0) ~/ 25.0; |
| 117 | + return depthFirstSearch(n, n); |
| 118 | + } |
| 119 | +} |
0 commit comments