Given a non-empty array containing only positive integers, find if The array can be partitioned into subsets such, the sum of elements in both subsets is equal. Note:each of the array element would not exceed 100 200 1:input: [ 1, 5, one, 5]output: true explanation:the array can be partitioned as [ 1, 5, 5] and [11 2:input: [ 1, 2, 3, 5]output: false explanation:the array cannot is partitioned into equal sum subsets.
Backpack Problem:dp[i][j] means if the first I elements can sum up to value J
DP[I][J] = Dp[i-1][j] | | (J>=nums[i-1] && dp[i-1][j-nums[i-1])
The result is if the half sum of the array can be summed to elements in the array
1 Public classSolution {2 Public BooleanCanpartition (int[] nums) {3 if(Nums.length = = 0)return true;4 intVolume = 0;5 for(intnum:nums) {6Volume + =num;7 }8 if(volume% 2 = = 1)return false;9Volume/= 2;Ten Boolean[] DP =New Boolean[Volume+1]; OneDp[0] =true; A for(intI=1; i<=nums.length; i++) { - for(intJ=volume; j>=0; j--) { -DP[J] = Dp[j] | | (J>=nums[i-1] && dp[j-nums[i-1]]); the } - } - returnDp[volume]; - } +}
Leetcode:partition Equal Subset Sum