Partition a set of numbers into two so that difference between their sum is minimum with equal size
来源:互联网 发布:厨房刀具推荐知乎 编辑:程序博客网 时间:2024/05/18 03:28
Partition a set of numbers into two such that difference between their sum is minimum, and both sets have equal number of elements.
For example: {1, 4, 9, 16} is partitioned as {1,16} and {4,9} with diff = 17-13=4.
Does greedy work here? First sorting, and then picking smallest and largest to fall in set 1, and picking 2nd smallest and 2nd largest to fall in set 2.
For example: {1, 4, 9, 16} is partitioned as {1,16} and {4,9} with diff = 17-13=4.
Does greedy work here? First sorting, and then picking smallest and largest to fall in set 1, and picking 2nd smallest and 2nd largest to fall in set 2.
I was asked to prove which I failed :(
----------------------------------------------------------------------------------------------------
The problem is similar to knapsack problem, however, the number of knapsack is constant in this problem, so :
Observing the insanity of above posts which claim to develop a greedy O(nlogn) solution for a variation of partition problem, I was compelled to code the program. Being NP-hard by nature, the solution falls into pseudo-polynomial time algorithm with complexity O(n^2W) where n = # of elements, W = sum of elements.
//constraints: n is evenvoid fun (int items[], int n){ int total = accumulate (items, items+n, 0) / 2; int maxSubsetSz = n/2 ; vector< vector<int> > T (maxSubsetSz+1, vector<int> (total+1, 0) ); //T[i][j] is set if there exists subset of size i that sum up to j T[0][0] = 1; for(int i = 0; i < n; i++) //consider taking i-th item for(int k = maxSubsetSz-1; k >= 0; k--) //each item must be taken once, hence loop backwards for(int j = 0; j <= total-items[i]; j++) if ( T[k][j] && items[i]+j <= total ) T[k+1][j+items[i]] = 1; for(int j = total; j >= 1; j--) if ( T [maxSubsetSz][j] ) { cout << "sum : " << j << endl; break; }}
0 0
- Partition a set of numbers into two so that difference between their sum is minimum with equal size
- How to divide a data set into equal size buckets
- It is confused that the difference of JSP with Servlet
- Is it possible to get the difference from two dynamic arrays regardless of their order?
- Split array into two equal sum subArray
- Find the number of subsets such that the sum of numbers in the subset is a prime number
- How to arrange 10 digits so that the product of the some of them is equal to a number represented by the remaining digits
- Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers
- A Minimum Volume Covering Approach with a Set of Ellipsoids
- Gson is a Java library that can be used to convert Java Objects into their JSON representation.
- [leetcode][two pointers] Minimum Size Subarray Sum
- minimum weighted difference in a set
- pushing a var of numbers that are strings into an array
- 《牛客网leetcode20题》Given a string s, partition s such that every substring of the partition is a palind
- Find number of subsets with equal sum
- Partition Equal Subset Sum
- Partition Equal Subset Sum
- Partition Equal Subset Sum
- ANSI编码是什么
- ,如果golang想成为主流语言,还需要解决哪些重要的问题
- HDU1715 大菲波数
- 《学习OpenCV》练习3-6
- HDU2048 神、上帝以及老天爷
- Partition a set of numbers into two so that difference between their sum is minimum with equal size
- HDU2049 不容易系列之(4)——考新郎
- opencv中图像显示的问题
- redis 安装及安装遇到的问题解决
- 第三周:继承
- 一个简单的测试框架(Javascript)
- 《学习OpenCV》练习3-7
- ln命令 (软链接)
- c语言中运算符优先级