Leetcode 837. New 21 | Python


837. New 21

Title Source: leetcode https://leetcode-cn.com/problems/new-21-game


Alice participates in a game based on the blackjack rule, which is described as follows:

Alice starts with 0 and draws numbers when her score is less than k. When extracting, she randomly gets an integer from the range of [1, w] as a fraction to accumulate, where W is an integer. Each extraction is independent and the result has the same probability.

When Alice gets no less than k, she stops taking numbers. What’s the probability of Alice’s score not exceeding n?

Example 1:

Input: n = 10, k = 1, w = 10
Output: 1.00000
Explanation: Alice gets a card and stops.

Example 2:

Input: n = 6, k = 1, w = 10
Output: 0.60000
Explanation: Alice gets a card and stops.
Under the six possibilities of W = 10, her score does not exceed n = 6.

Example 3:

Input: n = 21, k = 17, w = 10
Output: 0.73278


  • 0 <= K <= N <= 10000
  • 1 <= W <= 10000
  • If the error between the answer and the correct answer does not exceed 10 ^ – 5, the answer will be considered as the correct answer passed.
  • The judgment limit time for this problem has been reduced.

Thinking of solving problems

Idea: dynamic planning

In the topic, three variables are provided. N. K, W, what are the three?

N: Here is equivalent to a boundary, the requirements of the topic is the final number extraction and comparison with n to determine win or lose
K: Here is a condition to continue to extract numbers
W: The face value of the number, that is, the extracted number, is in the range of 1 to W

Now let’s look at example 1:

N = 10, K = 1, W = 10

Because Alice started with 0, at this time 0 < k = 1, then she can continue to draw numbers, and the face value range of numbers is [1,10]. What is the probability that the sum of numbers is less than n (n = 10)? It’s obvious here that the probability is 1. Because the face value is in [1,10], no matter which number is extracted, it will be greater than k, which meets the condition of not extracting, and the final sum will fall between [1,10]. The result here must be less than or equal to n. So the probability is one.

Let’s look at example 2

N = 6, K = 1, W = 10

What’s changed here is the value of n. In the case of extraction, as in example 1, no matter which number is extracted within the range of face value [1,10], it can not be extracted again, because the sum is greater than k, and the final sum also falls between [1,10]. However, the value of N here has changed to 6. Only the final number and the part falling in [1,6] can be consistent with N, which accounts for 60% of the total part, so the probability is 0.6.

Example 3 is more complicated. It is also a situation that we mainly need to analyze.

We can see that in the previous examples 1 and 2, Alice starts to extract the number with 0, then accumulates it with the extracted number, and then compares it with K and w to determine whether it can be extracted again and whether it does not exceed n?

In fact, it can be seen here that the probability that Alice can win is actually related to the score before the next round.

Now orderdp[i]It indicates the probability of winning when the number is extracted from the case of score I, then the final requirement isdp[0]The result of the test.

Now we need to work out the state transition equation. Let’s first look at some conditions given in the title.

When the score exceeds K, stop drawing numbers for settlement at this time. When the score does not exceed n, it will be judged as victory, otherwise it will be defeated.

Now, let’s take a look at the maximum score that can be extracted at the last time. Because it exceeds K, it cannot be extracted again. If you want to extract again, the maximum score allowed is k – 1. Then the largest number that can be extracted in the second extraction is w, so the maximum score is k – 1 + W.

That is to say, when $k-leq i-leq min (n, K + W-1) $, at this timedp[i]=1And $I > min (n, K + W-1) $,dp[i]=0

Now, how to calculate the value of DP [i] when $0 / Leq I < K $?

In fact, when $0 / Leq I < K $, the probability is whether the cumulative score exceeds the sum of the probabilities of N after the number is extracted. And the previous topic said that the probability of extraction between [1, w] numbers is equal. Then the state transfer equation is as follows:

dp[i] = \frac{dp[i+1]+dp[i+2]+…+dp[i+W]}{W}

Although the state transition equation has been solved, it will be found that substituting the state transition equation into the code will time out. The optimization is as follows:

Here, let’s first look at what DP [i + 1] looks like? According to the previous state transfer equation:

dp[i+1] = \frac{dp[i+2]+dp[i+3]+…+dp[i+W+1]}{W}

However, at this time $0  Leq I < k-1 $.

It can be seen that most of DP [i] and DP [i + 1] are the same, so the value of DP [i] can be obtained from DP [i + 1]:

dp[i]-dp[i+1] =\frac{dp[i+1]-dp[i+W+1]}{W}

At this point:

dp[i] = dp[i+1] – \frac{dp[i+W+1]- dp[i+1]}{W}

Here, $I = k – 1 $, which is not applicable to the above formula, then substitute it into the initial transfer equation:

dp[K-1] = \frac{dp[K]+dp[K+1]+dp[K+W-1]}{W}

We have a condition of $DP [i] = 1 $, that is, when I is in the range of $[K, min (n, K + W-1)] $.

At this point, when I is k-1, the part that may win by extracting the number again falls on the number extracted by $min (n, K + W-1) – K + 1 $

dp[K-1]=\frac{min(N, K+W-1)-K+1}{W}=\frac{min(N-K+1, W)}{W}

Other values can be obtained by a new transfer equation.

The specific code is as follows.

code implementation

class Solution:
    def new21Game(self, N: int, K: int, W: int) -> float:
        dp = [0.0] * (K+W)
        #First, the probability of 1.0 is dealt with
        #That is, I fall in [K, min (n, K + W-1)]
        for i in range(K, min(N, K+W-1) + 1):
            dp[i] = 1.0
        #Here we first calculate the case of K - 1
        #The derived formula is put here
        dp[K-1] = float(min(N-K+1, W) / W)
        #Here we start to calculate the value of DP [0] from K-2
        for i in range(K-2, -1, -1):
            dp[i] = dp[i+1] - (dp[i+W+1]-dp[i+1]) / W
        return dp[0]

Implementation results

Leetcode 837. New 21 | Python


  • The main difficulty here is to deduce the state transition equation.
  • According to the meaning of the title, the direction of derivation is from the following situation forward. First deal with the last extraction of numbers, and then analogy.
  • Whether the game can win or not is related to the score before the next extraction. The current probability of winning is actually the sum of the probabilities of whether the cumulative score exceeds n after the number is extracted. According to this, the state transfer equation can be obtained.
  • Because the initial transfer equation will time out, optimize it. According to the adjacent difference, the state transfer equation is simplified.

If you think the article is OK, you are welcome to pay attention. The official account of the book is updated simultaneously, and it is equally welcome.

Recommended Today

asp.net Application of regular expression

1. Balanced group / recursive matching (?’ Group ‘), which is called the corresponding content of group, and counts it on the stack;(?’- Group ‘), and count the corresponding content named group out of the stack(?!) Zero width negative look ahead assertion. Since there is no suffix expression, attempts to match always failRegular example:,{0,1}”5″:\[[^\[\]]*(((?’Open’\[)[^\[\]]*)+((?’-Open’\])[^\[\]]*)+)*(?(Open)(?!))\],{0,1} Test […]