-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path837.py
More file actions
33 lines (32 loc) · 1.04 KB
/
837.py
File metadata and controls
33 lines (32 loc) · 1.04 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
__________________________________________________________________________________________________
sample 68 ms submission
class Solution:
def new21Game(self, N, K, W):
"""
:type N: int
:type K: int
:type W: int
:rtype: float
"""
d=[0]*(K+max(W,N)+10)
for i in range(K,N+1):
d[i]=1
ms=N-K+1
for i in range(K-1,-1,-1):
d[i]=ms/W
ms+=d[i]-d[i+W]
return d[0]
__________________________________________________________________________________________________
sample 72 ms submission
class Solution:
def new21Game(self, N: int, K: int, W: int) -> float:
# dp[x] = the answer when Alice has x points
dp = [0]*(N+1+W)
for i in range(K, N+1):
dp[i] = 1.0
S = min(N-K+1, W)
for i in range(K-1, -1, -1):
dp[i] = S/W
S += dp[i] - dp[i+W]
return dp[0]
__________________________________________________________________________________________________