Skip to content

Commit 56b50db

Browse files
authored
Merge pull request #92 from remy727/0808-soup-servings
808. Soup Servings
2 parents cb1457d + 0ab9d45 commit 56b50db

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@
230230
| 785 | Is Graph Bipartite? | [Ruby](./algorithms/ruby/0785-is-graph-bipartite.rb) | Medium |
231231
| 790 | Domino and Tromino Tiling | [Ruby](./algorithms/ruby/0790-domino-and-tromino-tiling.rb) | Medium |
232232
| 802 | Find Eventual Safe States | [Ruby](./algorithms/ruby/0802-find-eventual-safe-states.rb) | Medium |
233+
| 808 | Soup Servings | [Ruby](./algorithms/python3/0808-soup-servings.py) | Medium |
233234
| 815 | Bus Routes | [Ruby](./algorithms/ruby/0815-bus-routes.rb) | Hard |
234235
| 837 | New 21 Game | [Ruby](./algorithms/ruby/0837-new-21-game.rb) | Medium |
235236
| 839 | Similar String Groups | [Ruby](./algorithms/ruby/0839-similar-string-groups.rb) | Hard |
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# 808. Soup Servings
2+
# Medium
3+
# https://leetcode.com/problems/soup-servings
4+
5+
import math;
6+
7+
class Solution:
8+
def soupServings(self, n: int) -> float:
9+
m = math.ceil(n / 25)
10+
dp = {}
11+
12+
def calculate_dp(i, j):
13+
return (dp[max(0, i - 4)][j] + dp[max(0, i - 3)][j - 1] +
14+
dp[max(0, i - 2)][max(0, j - 2)]
15+
+ dp[i - 1][max(0, j - 3)]) / 4
16+
17+
dp[0] = {0: 0.5}
18+
for k in range(1, m + 1):
19+
dp[0][k] = 1
20+
dp[k] = {0: 0}
21+
for j in range(1, k + 1):
22+
dp[j][k] = calculate_dp(j, k)
23+
dp[k][j] = calculate_dp(k, j)
24+
if dp[k][k] > 1 - 1e-5:
25+
return 1
26+
return dp[m][m]
27+
28+
# ********************#
29+
# TEST #
30+
# ********************#
31+
32+
import unittest
33+
34+
class TestStringMethods(unittest.TestCase):
35+
def test_addBinary(self):
36+
self.assertEqual(Solution.soupServings(self, 50), 0.62500)
37+
self.assertEqual(Solution.soupServings(self, 100), 0.71875)
38+
39+
40+
if __name__ == '__main__':
41+
unittest.main()

0 commit comments

Comments
 (0)