Open azl397985856 opened 6 months ago
class Solution: def new21Game(self, n: int, k: int, w: int) -> float: if k == 0 or n >= k + w - 1: return 1.0
dp = [0.0] * (k + w)
current_sum = 0.0
for i in range(k, k + w):
dp[i] = 1.0 if i <= n else 0.0
current_sum += dp[i]
for i in range(k - 1, -1, -1):
dp[i] = current_sum / w
current_sum = current_sum - dp[i + w] + dp[i]
return dp[0]
class Solution:
def new21Game(self, N: int, K: int, W: int) -> float:
dp = [0] * (K + W)
win_sum = 0
for i in range(K, K + W):
if i <= N:
dp[i] = 1
win_sum += dp[i]
for i in range(K - 1, -1, -1):
dp[i] = win_sum / W
win_sum += dp[i] - dp[i + W]
return dp[0]
class Solution:
def new21Game(self, n: int, k: int, maxPts: int) -> float:
dp = [0]*(k+maxPts)
my_sum = 0
for i in range(k,k+maxPts):
if i<=n:
dp[i]=1
my_sum += dp[i]
for i in range(k-1, -1, -1):
dp[i] = my_sum/maxPts
my_sum += dp[i]
my_sum += -dp[i+maxPts]
return dp[0]
class Solution {
public:
double new21Game(int n, int k, int maxPts) {
if (k == 0) {
return 1.0;
}
vector
837. 新 21 点
入选理由
暂无
题目地址
https://leetcode-cn.com/problems/new-21-game
前置知识
暂无
题目描述