- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0808-soup-servings.py
41 lines (32 loc) · 979 Bytes
/
0808-soup-servings.py
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
37
38
39
40
41
# 808. Soup Servings
# Medium
# https://leetcode.com/problems/soup-servings
importmath;
classSolution:
defsoupServings(self, n: int) ->float:
m=math.ceil(n/25)
dp= {}
defcalculate_dp(i, j):
return (dp[max(0, i-4)][j] +dp[max(0, i-3)][j-1] +
dp[max(0, i-2)][max(0, j-2)]
+dp[i-1][max(0, j-3)]) /4
dp[0] = {0: 0.5}
forkinrange(1, m+1):
dp[0][k] =1
dp[k] = {0: 0}
forjinrange(1, k+1):
dp[j][k] =calculate_dp(j, k)
dp[k][j] =calculate_dp(k, j)
ifdp[k][k] >1-1e-5:
return1
returndp[m][m]
# ********************#
# TEST #
# ********************#
importunittest
classTestStringMethods(unittest.TestCase):
deftest_addBinary(self):
self.assertEqual(Solution.soupServings(self, 50), 0.62500)
self.assertEqual(Solution.soupServings(self, 100), 0.71875)
if__name__=='__main__':
unittest.main()