【LeetCode】【837. New 21 Game】(python版)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_20141867/article/details/81261711

Description:
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:

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

思路:
题目的意思是,在已有点数不超过K的情况下,可以从[1, W]中任选一个数,保证和不超过N。求最后当点数超过K时,这个点数小于N的概率。这个问题其实类似于爬楼梯,每次可跨上[1, W]阶楼梯。
采用动态规划,dp[i]表示点数为i的概率,只有当已有点数小于K时,才能再取数,所以有:
当i<=K时,dp[i] = (前W个dp之和) / W
当K< i< K+W时,前一次最多只能是K-1点,最少为i-W点,所以dp[i] = (dp[K-1]+dp[K-2]+…+dp[i-W])/W
当i >= K+W时,无论如何都无法取到,dp[i] = 0

class Solution(object):
    def new21Game(self, N, K, W):
        """
        :type N: int
        :type K: int
        :type W: int
        :rtype: float
        """
        if K == 0: return 1
        dp = [1.0] + [0.0] * N
        # Wsum表示(前W个dp之和)/W
        Wsum = 1.0000
        for i in range(1, N + 1):
            dp[i] = Wsum / W
            # 因为当i>=K时,不能再取数,因此后面的概率不能累加
            if i < K: Wsum += dp[i]
            # 因为只需要计算前w个dp之和,所以当i>=W时,减去最前面的dp。
            if 0 <= i - W < K: Wsum -= dp[i - W]
        return sum(dp[K:])

猜你喜欢

转载自blog.csdn.net/qq_20141867/article/details/81261711