nikunjsoni

837

Jul 28th, 2021
582
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2. public:
  3.     double new21Game(int n, int k, int w) {
  4.         if(k == 0 || n >= k+w) return 1.0;
  5.         vector<double> dp(n+1);
  6.         dp[0] = 1.0;
  7.         double wSum=1.0, res=0.0;
  8.        
  9.         for(int i=1; i<=n; i++){
  10.             dp[i] = wSum/w;
  11.             if(i<k) wSum += dp[i];
  12.             if(i-w >= 0) wSum -= dp[i-w];
  13.         }
  14.         for(int i=k; i<=n; i++)
  15.             res += dp[i];
  16.         return res;
  17.     }
  18. };
RAW Paste Data