A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 837. New 21 Game, the solution in this repository is mainly a dynamic programming solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: dynamic programming, sliding window.
The notes already sitting in the source point us in the right direction:
- sliding window, dp
- https://leetcode.com/problems/new-21-game/discuss/132334/One-Pass-DP-O(N)
- time: O(N), space: O(N)
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are new21Game.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(N), space: O(N)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//sliding window, dp
02//https://leetcode.com/problems/new-21-game/discuss/132334/One-Pass-DP-O(N)
03//Runtime: 4 ms, faster than 81.06% of C++ online submissions for New 21 Game.
04//Memory Usage: 9.3 MB, less than 52.69% of C++ online submissions for New 21 Game.
05//time: O(N), space: O(N)
06class Solution {
07public:
08 double new21Game(int N, int K, int W) {
09 /*
10 stop when drawing no cards,
11 at this time, Alice has definitely <= N points
12 */
13 if(K == 0) return 1;
14 /*
15 since we stop drawing cards when we have K points,
16 we can draw cards when we have K-1 points,
17 and the maximum points we can get is K-1+W,
18 if K-1+W <= N, then we will have N or less points we prob 1
19 */
20 if(K-1+W <= N) return 1;
21
22 /*
23 stops at K, which is larger than N,
24 so the prob of having <= N ponits is 0
25 */
26 if(N < K) return 0;
27
28 vector<double> dp(N+1, 0);
29
30 //?
31 dp[0] = 1;
32 /*
33 we can go from [i-W, i-1] to i by drawing one card
34 Wsum is the sum of prob of dp[i-W] to dp[i-1]
35 */
36 double Wsum = 1; //?
37 /*
38 prob of getting points <= N,
39 this is equivalent to sum of prob of getting [K,N]
40 */
41 double res = 0;
42
43 for(int i = 1; i <= N; ++i){
44 /*
45 Wsum: sum of prob of reaching [i-W,i-1] points
46 *1/W: multiplied by the prob of drawing 1(or 2, 3, ..., W)
47 */
48 dp[i] = Wsum/W;
49 if(i < K){
50 /*
51 when we have i(less than K) points,
52 we can still draw card.
53 This is done by accumulating its prob to Wsum
54 */
55 Wsum += dp[i];
56 }else{
57 /*
58 when i reaches K, we stop drawing cards
59 res: sum of dp[K] to dp[N]
60 */
61 res += dp[i];
62 }
63 if(i-W >= 0) Wsum -= dp[i-W];
64 }
65
66 return res;
67 }
68};
Cost