Given a non-empty array containing only positive integers, find if the array can is partitioned into and subsets such that The sum of elements in both subsets is equal.
Note:
- Each of the array element would not be exceed 100.
- The array size would not be exceed 200.
Example 1:
Input: [1, 5, one, 5]output:trueexplanation:the array can be partitioned as [1, 5, 5] and [11].
Example 2:
Input: [1, 2, 3, 5]output:falseexplanation:the array cannot is partitioned into equal sum subsets.
Solution One:
The first thing to think about is not dynamic planning, but breadth-first search. First look at the topic, the array can be RIP premise is the array element and must be an even number. Then the subject is transformed into a previously done problem, that is, Leetcode 40. Combination sum ii--combined sum II, looking for a combination of elements that target the sum of the array elements. Also, each element can be used only once, as well as repeating elements. Just 40 is the result set of finding the path, and the subject just needs to find any one of them to meet the conditions of the combination. Code copy, slightly modified.
After submitting the speed beat 96.3%, prove that it is possible to do so. But there are some top-ranked answers that use the idea of Dfs but are not sorted, where the search for the next element is changed from the tail of the sequence to the forward search. After testing I found that the test case of the problem should be sorted. If you copy the code that has not been sorted with Dfs thought, then test the example {100, 1, 1, ... (A total of 98 1)} , it will commit the timeout. The Leetcode test case is not complete, and some people bore the loophole. Analyze why it timed out, because if not sorted, the amount of heavy work will be very large. If you don't, you'll be validating the same set of results over and over again, and it will time out. So this problem must go to heavy, go to the weight must be sorted to not time out.
Solution Two:
This problem can also be used for dynamic planning, the number of each traversal of the put and do not put in the result set of the state are saved up. A bit like knapsack problem, each time put or not put a number in will affect the future can be put into the number.
Solution One (Java)
classSolution { Public BooleanCanpartition (int[] nums) { intsum = 0; for(intnum:nums) Sum+=num; if(sum & 1) = = 1)return false; Sum>>= 1; Arrays.sort (Nums); //sort to go to the heavy returnDFS (nums, 0, sum); } Private BooleanDfsint[] Nums,intIinttarget) { if(target = = 0)return true; if(I > Nums.length | | target < 0)return false; BooleanMark =false;//the list that corresponds to the 40th question, which stores the path here for(intp = i; P < nums.length; p++) { intNewsum = Target-nums[p];//Add to PathMark = Mark | | DFS (Nums, p+1, newsum);//mark itself is equivalent to backtracking and is combined with the result of searching for the next node while(P < nums.length-1 && nums[p] = = nums[p+1]) p++;//go heavy, take advantage of sorting features } returnMark; }}
Solution Two (Java)
classSolution { Public BooleanCanpartition (int[] nums) { intsum = 0; for(intnum:nums) Sum+=num; if(sum & 1) = = 1)return false; Sum>>= 1; Boolean[] DP =New Boolean[Sum + 1]; dp[0] =true; for(intj = 0; J < Nums.length; J + +) { for(inti = sum; I >=nums[j]; i--) Dp[i]= Dp[i] | | dp[i-Nums[j]]; if(Dp[sum])return true; } returnDp[sum]; }}
(Java) Leetcode 416. Partition Equal subset sum--segmentation and subsets