Advertisement
nikunjsoni

808

Jun 13th, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     double soupServings(int N) {
  4.         if(N > 4275) return 1.0;
  5.         int size = (N+24)/25;
  6.         vector<vector<double>> dp(size+1, vector<double>(size+1, 0));
  7.         dp[size][size] = 1;
  8.         for(int i = size; i >= 1; --i) {
  9.             for(int j = size; j >= 1; --j) {
  10.                 if(i != 0) dp[max(0, i-4)][j] += 0.25*dp[i][j];
  11.                 if(i != 0 && j != 0) {
  12.                     dp[max(0, i-3)][max(0, j-1)] += 0.25*dp[i][j];
  13.                     dp[max(0, i-2)][max(0, j-2)] += 0.25*dp[i][j];
  14.                     dp[max(0, i-1)][max(0, j-3)] += 0.25*dp[i][j];
  15.                 }
  16.             }
  17.         }
  18.         double p = 0.5*dp[0][0];
  19.         for(int j = 1; j <= size; ++j) p += dp[0][j];
  20.         return p;
  21.     }
  22. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement