Swap the elements of two sequences, such that the difference of the element-sums gets minimal.
来源:互联网 发布:知乎 大误 编辑:程序博客网 时间:2024/06/04 19:38
Swap the elements of two sequences, such that the difference of the element-sums gets minimal.
An interview question:
Given two non-ordered integer sequences
a
andb
, their size is n, all numbers are randomly chosen: Exchange the elements ofa
andb
, such that the sum of the elements ofa
minus the sum of the elements ofb
is minimal.
Given the example:
a = [ 5 1 3 ]b = [ 2 4 9 ]
The result is (1 + 2 + 3) - (4 + 5 + 9) = -12.
My algorithm: Sort them together and then put the first smallest n
ints in a
and left in b
. It is O(n lg n) in time and O(n) in space. I do not know how to improve it to an algorithm with O(n) in time and O(1) in space. O(1) means that we do not need more extra space except seq 1 and 2 themselves.
Any ideas ?
An alternative question would be: What if we need to minimize the absolute value of the differences (minimize |sum(a) - sum(b)|
)?
A python or C++ thinking is preferred.
2 Answers
Revised solution:
Merge both lists x = merge(a,b).
Calculate median of x (complexity O(n) See http://en.wikipedia.org/wiki/Selection_algorithm )
Using this median swap elements between a and b. That is, find an element in a that is less than median, find one in b that is more than median and swap them
Final complexity: O(n)
Minimizing absolute difference is NP complete since it is equivalent to the knapsack problem.
What comes into my mind is following algorithm outline:
- C = A v B
- Partitially sort #A (number of A) Elements of C
- Subtract the sum of the last #B Elements from C from the sum of the first #A Elements from C.
You should notice, that you don't need to sort all elements, it is enough to find the number of A smallest elements. Your example given:
- C = {5, 1, 3, 2, 4, 9}
- C = {1, 2, 3, 5, 4, 9}
- (1 + 2 + 3) - (5 + 4 + 9) = -12
A C++ solution:
#include <iostream>#include <vector>#include <algorithm>int main(){ // Initialize 'a' and 'b' int ai[] = { 5, 1, 3 }; int bi[] = { 2, 4, 9 }; std::vector<int> a(ai, ai + 3); std::vector<int> b(bi, bi + 3); // 'c' = 'a' merged with 'b' std::vector<int> c; c.insert(c.end(), a.begin(), a.end()); c.insert(c.end(), b.begin(), b.end()); // partitially sort #a elements of 'c' std::partial_sort(c.begin(), c.begin() + a.size(), c.end()); // build the difference int result = 0; for (auto cit = c.begin(); cit != c.end(); ++cit) result += (cit < c.begin() + a.size()) ? (*cit) : -(*cit); // print result (and it's -12) std::cout << result << std::endl;}
- Swap the elements of two sequences, such that the difference of the element-sums gets minimal.
- Empire of the sums
- swap two value of the same type
- The Elements of Style
- The Elements of Style
- The Elements of Style
- The difference of gets(),getch(),getchar() and getline()
- Find the k-th minimal element of an unsorted array
- The efficiency of swap
- It is confused that the difference of JSP with Servlet
- Find the top k sums of two sorted arrays
- Given an array of integers, return indices of the two numbers such that they add up to a specific ta
- The difference of the type of DB type property such char,varchar,nchar and nvarchar
- The Elements of C++ Style
- The Elements of Programming Style
- The Elements of Programming Style
- The Elements of Programming Style
- The Elements of Programming Style
- IT术语
- hdu 1009
- 2013年3月博客行业网站综合影响力排名
- js的Prototype属性 解释及常用方法
- java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' bef
- Swap the elements of two sequences, such that the difference of the element-sums gets minimal.
- 新婚前的谋杀(来自推理世界A 2013/04 作者 炼弧),谜题篇
- 随机森林
- Java Socket实战之四 传输压缩对象
- USB扫描枪设置流程 扫描枪使用
- 提高java代码性能总结
- 关于jar命令创建可运行包的小探究
- 交通诱导屏坏点统计表2013-02-03.csv
- ZOJ 1152 A Mathematical Curiosity