0837.cpp (434B)
1 class Solution { 2 public: 3 double new21Game(int n, int k, int maxPts) { 4 if (k == 0 || n >= k + maxPts) return 1.0; 5 vector<double> dp(n + 1); 6 double sum = 1.0, ans = 0.0; 7 8 dp[0] = 1.0; 9 for (int i = 1; i < n + 1; i++) { 10 dp[i] = sum / maxPts; 11 (i < k ? sum : ans) += dp[i]; 12 if (i - maxPts >= 0) sum -= dp[i - maxPts]; 13 } 14 15 return ans; 16 } 17 };