Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int getMoneyAmount(int n) {
  4.        vector<vector<int>> dp(n + 2,vector<int>(n + 2));
  5.        
  6.        for(int l = n; l >= 1; l--){
  7.            for(int r = 1; r <= n; r++){
  8.                 int &memo = dp[l][r];
  9.                 if (l >= r){
  10.                     memo = 0;
  11.                 }else {
  12.                     int ans = INT_MAX;
  13.  
  14.                     for(int i = l; i <= r; i++){
  15.                         ans = min(ans,i + max(dp[i + 1][r],dp[l] [i - 1]));    
  16.                     }
  17.  
  18.                     memo = ans;
  19.                 }
  20.            }
  21.        }
  22.        return dp[1][n];
  23.     }
  24. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement