Partition problem is to determine whether a given set can be partitioned into two subsets such that the sum of elements in both subsets is same.
Examples
arr[] = {1, 5, 11, 5}
Output: true
The array can be partitioned as {1, 5, 5} and {11}
arr[] = {1, 5, 3}
Output: false
The array cannot be partitioned into equal sum sets.
Following are the two main steps to solve this problem:
1) Calculate sum of the array. If sum is odd, there can not be two subsets with equal sum, so return false.
2) If sum of array elements is even, calculate sum/2 and find a subset of array with sum equal to sum/2.
The first step is simple. The second step is crucial, it can be solved either using recursion or Dynamic Programming.
动态规划解决分割问题
本文介绍了一种利用动态规划解决分割问题的方法,该问题旨在判断一个整数数组是否可以分割成两个子集,使得两个子集的元素之和相等。文章首先检查数组总和是否为偶数,接着通过动态规划寻找是否存在子集使元素和等于总和的一半。
2203

被折叠的 条评论
为什么被折叠?



