aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-08-08 11:43:46 +0200
committerJack Sangdahl <[email protected]>2025-08-08 11:43:46 +0200
commitf878bb71563de831927bdf52b44e2dcc0ab4c577 (patch)
tree1db19de3971c90fbfb230d884aa3293187d49f9b
parent3b6f252059951a3a0927dfc15993ce1382f70e76 (diff)
downloadleetcode-f878bb71563de831927bdf52b44e2dcc0ab4c577.tar.gz
leetcode-f878bb71563de831927bdf52b44e2dcc0ab4c577.zip
808. soup servings
-rw-r--r--soup_servings.c39
-rw-r--r--soup_servings.py48
2 files changed, 64 insertions, 23 deletions
diff --git a/soup_servings.c b/soup_servings.c
index 68a3d9d..6449342 100644
--- a/soup_servings.c
+++ b/soup_servings.c
@@ -1,10 +1,9 @@
// 808. Soup Servings
-#include <stdio.h>
-#include <stdlib.h>
+#include "leetcode.h"
/*
- * 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:
+ * there are two ops of soup: op a, and op b. initially, we have 'n' ml of
+ * each op of soup. there are four kinds of operations:
* 1. serve 100 ml of soup a and 0ml of soup b
* 2. serve 75ml of soup a and 25ml of soup b
* 3. serve 50ml of soup a and 50ml of soup b
@@ -13,36 +12,30 @@
* each turn, we will choose from the four operations with an equal probability
* of '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
+ * some quantity of both ops 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.
*/
-double process(int a, int b, double **record) {
+double probability(int a, int b) {
+ if (a <= 0 && b > 0)
+ return 1;
if (a <= 0 && b <= 0)
return 0.5;
- if (a <= 0)
- return 1.0;
- if (b <= 0)
+ if (b <= 0 && a > 0)
return 0;
- if (record[a][b])
- return record[a][b];
- record[a][b] =
- 0.25 * (process(a - 4, b, record) + process(a - 3, b - 1, record) +
- process(a - 2, b - 2, record) + process(a - 1, b - 3, record));
- return record[a][b];
+ double op1 = probability(a - 100, b);
+ double op2 = probability(a - 75, b - 25);
+ double op3 = probability(a - 50, b - 50);
+ double op4 = probability(a - 25, b - 75);
+ return 0.25 * (op1 + op2 + op3 + op4);
}
double soupServings(int n) {
- double **record = malloc(150 * sizeof(double *));
- for (int i = 0; i < 150; i++)
- record[i] = calloc(150, sizeof(double));
- double ans = n >= 3600 ? 1.0 : process((n + 24) / 25, (n + 24) / 25, record);
- for (int i = 0; i < 150; i++)
- free(record[i]);
- free(record);
- return ans;
+ if (n >= 5000)
+ return 1;
+ return probability(n, n);
}
int main() {
diff --git a/soup_servings.py b/soup_servings.py
new file mode 100644
index 0000000..15d7eaf
--- /dev/null
+++ b/soup_servings.py
@@ -0,0 +1,48 @@
+# 808. Soup Servings
+
+"""
+there are two ops of soup: op a, and op b. initially, we have 'n' ml of
+each op of soup. there are four kinds of operations:
+1. serve 100 ml of soup a and 0ml of soup b
+2. serve 75ml of soup a and 25ml of soup b
+3. serve 50ml of soup a and 50ml of soup b
+4. serve 25ml of soup a and 75ml 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
+of '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 ops 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.
+"""
+
+
+class Solution(object):
+ def probability(self, a, b):
+ if a <= 0 and b > 0:
+ return 1
+ if a <= 0 and b <= 0:
+ return 0.5
+ if b <= 0 and a > 0:
+ return 0
+ op1 = self.probability(a - 100, b)
+ op2 = self.probability(a - 75, b - 25)
+ op3 = self.probability(a - 50, b - 50)
+ op4 = self.probability(a - 25, b - 75)
+ return 0.25 * (op1 + op2 + op3 + op4)
+
+ def soupServings(self, n):
+ """
+ :type n: int
+ :rtype: float
+ """
+ if n > 5000:
+ return 1.0
+ return self.probability(n, n)
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.soupServings(50))
+ print(obj.soupServings(100))