avatar
讨论几道M家的题# JobHunting - 待字闺中
D*e
1
1. Given n arrays, find n number such that sum of their differences is
minimum. For e.g. if there are three arrays
A = {4, 10, 15, 20}
B = {1, 13, 29}
C = {5, 14, 28}
find three numbers a, b, c such that |a-b| + |b-c| + |c-a| is minimum. Here
the answer is a = 15, b = 13, and c = 14
2. Given an array of +ve and -ve integers, re-arrange it so that u have +ves
on one end and -ves on other,BUT RETAIN ORDER OF APPEARANCE..
for eg,
1,7,-5,9,-12,15
ans=
-5,-12,1,7,9,15
do it in O(n) without using any extra space.
3.Given two sorted positive integer arrays A(n) and B(n), we define a set S
= {(a,b) | a \in A and b\in B}. Obviously there are n2 elements in S. The
value of such a pair is defined as Val(a,b) = a + b. Now we want to get the
n pairs from S with largest values.
avatar
f*t
2
第一题版里讨论过,没人给出正确答案
第二题版里也讨论过,据说有人证明了要keep order的话不可能O(n)实现
第三题难道不是像merge一样从两边最大的开始挑?
avatar
S*I
3
1.
a) Make a duplicate of the first array and put it after the last array.
b) Create a graph, let each node in the graph represent an element in each
arrays, add an edge between each node pair in adjacent arrays with weight as
absolute difference between these two nodes.
c) create a super source s which connects to all elements in the first array
with edges having equal weight and create a super destination d which
connects to all elements in the last array with edges having equal weight.
d) Find shortest path from s to d.
2. Can't figure it out.
3. Suppose both arrays are in decreasing order.
int count = 0;
int largest[n];
int i = 0, j = 0;
largest[count++] = a[i] + b[i];
while (count < n){
int max1 = a[i] + b[j + 1];
int max2 = a[i + 1] + b[j];
if(max1 > max2){
largest[count++] = max1;
j++;
}
else{
largest[count++] = max2;
i++;
}
}

Here
ves

【在 D*******e 的大作中提到】
: 1. Given n arrays, find n number such that sum of their differences is
: minimum. For e.g. if there are three arrays
: A = {4, 10, 15, 20}
: B = {1, 13, 29}
: C = {5, 14, 28}
: find three numbers a, b, c such that |a-b| + |b-c| + |c-a| is minimum. Here
: the answer is a = 15, b = 13, and c = 14
: 2. Given an array of +ve and -ve integers, re-arrange it so that u have +ves
: on one end and -ves on other,BUT RETAIN ORDER OF APPEARANCE..
: for eg,

avatar
D*e
4
1. the difference is pair-wise, not just adjacent ones
2. only think of nlog(n)
3. your logic is incorrect

as
array

【在 S**I 的大作中提到】
: 1.
: a) Make a duplicate of the first array and put it after the last array.
: b) Create a graph, let each node in the graph represent an element in each
: arrays, add an edge between each node pair in adjacent arrays with weight as
: absolute difference between these two nodes.
: c) create a super source s which connects to all elements in the first array
: with edges having equal weight and create a super destination d which
: connects to all elements in the last array with edges having equal weight.
: d) Find shortest path from s to d.
: 2. Can't figure it out.

avatar
B*1
5
1. A = {4, 10, 15, 20}
B = {1, 13, 29}
C = {5, 14, 28}
1. 每行取一个,排序,|a-b| + |b-c| + |c-a| =2*(max-min),更新全局最小的值
2. 扔掉min,从拿min那组数里面拿一个,跳到1执行,
如此反复执行,直到有一行所有数拿光了
用以上例子运行如下
1.{4,1,5} -> {1,4,5} -> answer = 2 *(5-1) = 8
2. 扔掉1,拿13 {4,5,13}-> answer = 2 *(13-4) = 18
3.扔掉4,拿10,{5,10,13} -> answer = 2 *(13-5) = 16;
4.扔掉 5拿14 {10,13,14} -> answer = 2*(14-10) = 8
5. 扔掉10 拿15,{13,1,4,15}-> answer = 2*(15-13) = 4
6.扔掉13,拿29  {14,15,29} -> answer = 2*(29-14) = 30
7. 扔掉 14拿28  {15,28,29} -> answer = 2*(29-15) = 28
8. 扔掉 15......
证明:假设3个数a>b>c,|a-b| + |b-c| + |c-a| =2*(a-c)
如果从a那里去,显然2*(a'-c)>2*(a-c)
如果从b的数组里面取,如果b'< a 结果仍然为2*(a-c)
如果b' > a,结果比2*(a-c)大
只有从c那个数组拿,才有可能得出更加小的值。
第3题在这里
. http://www.mitbbs.com/article_t/JobHunting/31919863.html
avatar
r*g
6
http://www.mitbbs.com/article_t/JobHunting/31707145.html

的值

【在 B*******1 的大作中提到】
: 1. A = {4, 10, 15, 20}
: B = {1, 13, 29}
: C = {5, 14, 28}
: 1. 每行取一个,排序,|a-b| + |b-c| + |c-a| =2*(max-min),更新全局最小的值
: 2. 扔掉min,从拿min那组数里面拿一个,跳到1执行,
: 如此反复执行,直到有一行所有数拿光了
: 用以上例子运行如下
: 1.{4,1,5} -> {1,4,5} -> answer = 2 *(5-1) = 8
: 2. 扔掉1,拿13 {4,5,13}-> answer = 2 *(13-4) = 18
: 3.扔掉4,拿10,{5,10,13} -> answer = 2 *(13-5) = 16;

avatar
m*q
7
第三题就是young tableaux
用一个最大堆,复杂度为O(nlgn);
或者不断调用youngify,复杂度为O(n^2)

Here
ves

【在 D*******e 的大作中提到】
: 1. Given n arrays, find n number such that sum of their differences is
: minimum. For e.g. if there are three arrays
: A = {4, 10, 15, 20}
: B = {1, 13, 29}
: C = {5, 14, 28}
: find three numbers a, b, c such that |a-b| + |b-c| + |c-a| is minimum. Here
: the answer is a = 15, b = 13, and c = 14
: 2. Given an array of +ve and -ve integers, re-arrange it so that u have +ves
: on one end and -ves on other,BUT RETAIN ORDER OF APPEARANCE..
: for eg,

avatar
D*e
8
要是只有三个数组,是可以这样分析

的值

【在 B*******1 的大作中提到】
: 1. A = {4, 10, 15, 20}
: B = {1, 13, 29}
: C = {5, 14, 28}
: 1. 每行取一个,排序,|a-b| + |b-c| + |c-a| =2*(max-min),更新全局最小的值
: 2. 扔掉min,从拿min那组数里面拿一个,跳到1执行,
: 如此反复执行,直到有一行所有数拿光了
: 用以上例子运行如下
: 1.{4,1,5} -> {1,4,5} -> answer = 2 *(5-1) = 8
: 2. 扔掉1,拿13 {4,5,13}-> answer = 2 *(13-4) = 18
: 3.扔掉4,拿10,{5,10,13} -> answer = 2 *(13-5) = 16;

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。