Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.
Note:
Each of the array element will not exceed 100.
The array size will not exceed 200.
Example 1:
Example 2:
Approach #1: DP. [C++]
Analysis:
dp[i][j] : whether we can sum to j using first i numbers.
dp[i][j] = true if dp[i-1][j-num]
check dp[n-1][sum/2]
init dp[-1][0] = true
Time complexity: O(n^2*sum) -> O(n*sum)
Space complexity: O(sum)
永远渴望,大智若愚(stay hungry, stay foolish)