LeetCode 837. New Blackjack

Original title link

Thinking about the problem of dynamic planning, consider from the back to the front.
When optimizing the time complexity, the same part of the two items before and after is eliminated.
Regarding the method of finding the state transition equation, one point of insight is: first determine the array form. The one-dimensional array finds the context, and the two-dimensional array finds the neighbor relationship.

Code:

class Solution {
    
    
public:
    double new21Game(int N, int K, int W) {
    
    

        if(K == 0) return 1.0;
        vector<double> res(K + W);
        for(int i = K; i <= N; i++) res[i] = 1.0;

        res[K - 1] = 1.0 * min(N - K + 1,W) / W;
        for(int i = K - 2; i >=0; i--){
    
    
            res[i] = res[i + 1] - (res[i + W + 1] - res[i + 1]) / W;
        }
        return res[0];
    }
};

Guess you like

Origin blog.csdn.net/qq_43078427/article/details/114541220