Advertisement
nikunjsoni

1049

Jun 11th, 2021
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.42 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int lastStoneWeightII(vector<int>& stones) {
  4.         int sum = 0;
  5.         for(int s:stones){
  6.             sum += s;
  7.         }
  8.         vector<int> dp(sum/2+1);
  9.         for(int i=0; i<stones.size(); i++){
  10.             for(int j=sum/2; j >=stones[i]; j--){
  11.                 dp[j] = max(dp[j] , dp[j-stones[i]] + stones[i]);
  12.             }
  13.         }
  14.         return sum - (2*dp[sum/2]);
  15.     }
  16. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement