Alice plays the following game, loosely based on the card game "21".
Alice starts with 0 points, and draws numbers while she has less than K points.  During each draw, she gains an integer number of points randomly from the range [1, W], where W is an integer.  Each draw is independent and the outcomes have equal probabilities.
Alice stops drawing numbers when she gets K or more points.  What is the probability that she has N or less points?
Example 1:
Input: N = 10, K = 1, W = 10
Output: 1.00000
Explanation:  Alice gets a single card, then stops.

Example 2:
Input: N = 6, K = 1, W = 10
Output: 0.60000
Explanation:  Alice gets a single card, then stops.
In 6 out of W = 10 possibilities, she is at or below N = 6 points.

Example 3:
Input: N = 21, K = 17, W = 10
Output: 0.73278
Note:

0 <= K <= N <= 10000
1 <= W <= 10000
Answers will be accepted as correct if they are within 10^-5 of the correct answer.
The judging time limit has been reduced for this question.

Source: leetcode | Difficulty: MEDIUM_HARD
Tags: Dynamic programming

Amortized analysis

Mathematics

Probability

Starter code: class Solution:
    def new21Game(self, N: int, K: int, W: int) -> float:
        

Solutions:
class Solution:

	def new21Game(self, N: int, K: int, W: int) -> float:
		dp = [0] * (N + W)
		for i in range(K, N + 1):
			dp[i] = 1
		S = min(W, N - K + 1)
		for i in range(K - 1, -1, -1):
			dp[i] = S / W
			S += dp[i] - dp[i + W]
		return dp[0]


class Solution(object):

	def new21Game(self, N, K, W):
		dp = [0.0] * (N + W + 1)
		for k in range(K, N + 1):
			dp[k] = 1.0
		S = min(N - K + 1, W)
		for k in range(K - 1, -1, -1):
			dp[k] = S / W
			S += dp[k] - dp[k + W]
		return dp[0]


class Solution:

	def new21Game(self, N: int, K: int, W: int) -> float:
		dp = [0.0] * (N + W + 1)
		for k in range(K, N + 1):
			dp[k] = 1.0
		S = sum((dp[i] for i in range(K, K + W + 1)))
		for k in reversed(list(range(K))):
			dp[k] = S / float(W)
			S += dp[k] - dp[k + W]
		return dp[0]


class Solution1:

	def new21Game(self, N: int, K: int, W: int) -> float:
		dp = [None] * (K + W)
		s = 0
		for i in range(K, K + W):
			dp[i] = 1 if i <= N else 0
			s += dp[i]
		for i in range(K - 1, -1, -1):
			dp[i] = s / W
			s = s - dp[i + W] + dp[i]
		return dp[0]

class Solution:

	def new21Game(self, N: int, K: int, W: int) -> float:
		dp = [1 if i <= N else 0 for i in range(K + W)]
		s = sum(dp[K:K + W])
		for i in range(K - 1, -1, -1):
			dp[i] = s / W
			s = s - dp[i + W] + dp[i]
		return dp[0]


class Solution:

	def new21Game(self, N: int, K: int, W: int) -> float:
		if K == 0:
			return 1
		dp = [0] * (W + K)
		dp[0] = 1
		sliding_sum = 1
		for i in range(1, K + W):
			dp[i] = sliding_sum / W
			if i < K:
				sliding_sum += dp[i]
			if i - W >= 0:
				sliding_sum -= dp[i - W]
				dp[i - W] = 0
		return sum(dp[K:N + 1]) / sum(dp)


class Solution:

	def new21Game(self, N: int, K: int, W: int) -> float:
		if K == 0:
			return 1
		dp = [0]
		(cur, temp) = (1, 1 / W)
		for i in range(1, W + 1):
			dp.append(cur / W)
			if i < K:
				cur += dp[-1]
		total = sum(dp[1:min(W + 1, K)])
		left = 1
		for i in range(W + 1, W + K):
			dp.append(total / W)
			total -= dp[left]
			left += 1
			if i < K:
				total += dp[-1]
		end = sum(dp[K:])
		good_end = sum(dp[K:N + 1])
		return good_end / end


class Solution:

	def new21Game(self, N: int, K: int, W: int) -> float:
		if K == 0:
			return 1
		running_sum = 1
		P = [1]
		for i in range(1, W):
			P.append(1 / W * running_sum)
			running_sum += P[-1]
		for i in range(W, K):
			P.append(1 / W * running_sum)
			running_sum += P[-1] - P[i - W]
		res = 0
		running_sum = sum(P[max(0, K - W):K])
		print(sum(P[2:12]) + sum(P[3:12]) + sum(P[4:12]) + sum(P[5:12]) + sum(P[6:12]) + sum(P[7:12]) + sum(P[8:12]) + sum(P[9:12]) + sum(P[10:12]) + sum(P[11:12]))
		for i in range(max(0, K - W), K):
			res += P[i] / W * max(min(i + W, N) - max(i + 1, K) + 1, 0)
		return res


class Solution:

	def new21Game(self, N: int, K: int, W: int) -> float:
		if N > K + W - 1 or K == 0:
			return 1
		dp = [0] * (K + W)
		dp[1] = 1 / W
		for i in range(2, K + 1):
			if i == W + 1:
				dp[i] = dp[i - 1] + (dp[W] - 1) / W
			else:
				dp[i] = ((W + 1) * dp[i - 1] - (dp[i - 1 - W] if i >= W + 1 else 0)) / W
		for i in range(K + 1, K + W):
			dp[i] = dp[i - 1] - (dp[i - W - 1] if i - W >= 2 else 0) / W
			if i == W + 1:
				dp[i] -= 1 / W
		res = sum((dp[i] for i in range(K, N + 1))) / sum((dp[i] for i in range(K, K + W)))
		return res


class Solution:

	def new21Game(self, N: int, K: int, W: int) -> float:
		if N >= K + W - 1:
			return 1
		if N < K:
			return 0
		if K == 0:
			return 1
		probs = [0] * (W + 2)
		probs[0] = 1
		probs[1] = 1 / W
		for i in range(1, K):
			probs[(i + 1) % (W + 2)] = probs[i % (W + 2)] * (W + 1) / W - probs[(i + 2) % (W + 2)] / W
		for i in range(W):
			probs[(K + i + 1) % (W + 2)] = probs[(K + i) % (W + 2)] - probs[(K + i + 2) % (W + 2)] / W
		if K % (W + 2) < (N + 1) % (W + 2):
			return sum(probs[K % (W + 2):(N + 1) % (W + 2)])
		else:
			return sum(probs[K % (W + 2):]) + sum(probs[:(N + 1) % (W + 2)])


class Solution:

	def new21Game(self, N: int, K: int, W: int) -> float:
		if N < K:
			return 0
		if N > K - 1 + W:
			return 1
		dp = [0] * (K + W)
		for k in range(K, N + 1):
			dp[k] = 1
		S = N - K + 1
		for k in range(K - 1, -1, -1):
			dp[k] = S / W
			S += dp[k] - dp[k + W]
		return dp[0]


class Solution:

	def new21Game(self, N: int, K: int, W: int) -> float:
		if K == 0:
			return 1.0
		if K - 1 + W <= N:
			return 1.0
		prob = [0.0] * (K + W)
		avg = 0
		for i in range(W):
			prob[K + i] = float(K + i <= N)
			avg += prob[K + i] / W
		for i in range(K)[::-1]:
			prob[i] += avg
			avg += (prob[i] - prob[i + W]) / W
		return prob[0]