Minimum Difference Subsets!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| int Solution::solve(vector<int> &A) { int sum = 0; for(auto a : A) sum += a; vector<int> dp((sum + 1) / 2 + 1,0); dp[0] = 1; int res = sum; for(auto a : A) { for(int j = dp.size() - 1; j >= a; j--) { if(dp[j-a]) dp[j] = 1; if(dp[j]) res = min(res, abs(sum - 2 * j)); } } return res; }
|