由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 这道题怎么解
相关主题
贡献一道面试题目:找出在一个数组里面只出现1次的两个数字今天竟然把MULTIPLY(大数相乘)写完了,发帖庆祝
一道要求常数空间和O(n)时间的排序题如何求一个整数阶乘的各位数字和
L家phone面,悲剧来发个我的Leetcode的Python答案吧
大牛给个大数(+-*)的面试解答吧发一个Startup的面经 - Affirm
上一道我以前喜欢出的题目吧贴几道老题目
F面经在整数数组中加运算符号和括号,求max
问一道前几天在版上看见的题关于Leetcode
大整数相乘谁贴个bug free的code近来比较重复的问题, 求解
相关话题的讨论汇总
话题: dp话题: int话题: len话题: num1话题: num2
进入JobHunting版参与讨论
1 (共1页)
p**********e
发帖数: 316
1
there are two groups of people; n number of A's & n of B's. We want to have
n pairs of A and B from two groups and to minimize the sum of the weight
differences.
EG
A[]={5,3,4};
B[]={1,6,2};
min = 5
3-1, 4-2,5-6
l******6
发帖数: 340
2
sort both A and B
Assign number with same idx in A and B into a pair
g*********e
发帖数: 14401
3
greedy algorithm
then prove it's optimal
l******6
发帖数: 340
4

example
A 2 , 6
B 3 , 0
How greedy works?

【在 g*********e 的大作中提到】
: greedy algorithm
: then prove it's optimal

g*********e
发帖数: 14401
5

sort A, B then match each pair 6-3, 2-0
given(A1 < A2), (B1 < B2)
cost((A1, B1), (A2, B2)) is always no greater than cost((A1, B2), (A2,B1))

【在 l******6 的大作中提到】
:
: example
: A 2 , 6
: B 3 , 0
: How greedy works?

p**********e
发帖数: 316
6
怎么证明这种解法就是最小呢

【在 l******6 的大作中提到】
: sort both A and B
: Assign number with same idx in A and B into a pair

c***0
发帖数: 449
7
先计算两个数组和的差, 再算算每对数组元素之间的差, 如果有某对的差在0与和的
差之间,那就找出最接近和的差的一半的那一对,进行对调,然后重新计算新的数组的
差,以此递归,直到找不出任何一对的差在0与和的差之间为止。

【在 g*********e 的大作中提到】
:
: sort A, B then match each pair 6-3, 2-0
: given(A1 < A2), (B1 < B2)
: cost((A1, B1), (A2, B2)) is always no greater than cost((A1, B2), (A2,B1))

r*c
发帖数: 167
8
I tend to believe either Greedy or DP works. Here is a DP solution, not
fully tested. Greedy is preferred, as it is cleaner and shorter.
int minPairDistSum(const vector num1, const vector num2) {
int len = num1.size();
assert(len == num2.size());
if(len <= 0) return INT_MIN;
vector >dp(len + 1, vector(len +1, 0));
for(int i=0; i<=len; ++i){
for(int j=0; j<=len; ++j){
dp[i][j] = INT_MAX;
if(i==j && j==0) continue;
if(i==0) {dp[i][j] = num1[j -1]; continue;}
if(j==0) {dp[i][j] = num2[i -1]; continue;}
if(i != j)
dp[i][j] = num1[j-1] - num2[i-1];
else {
if(i ==1) {
dp[i][j] = num1[i-1]-num2[i-1];
continue;
}
int alt0 = dp[i-1][j-1] + num1[i-1]-num2[i-1];
if(i ==2) {
dp[i][j] = std::min(alt0, dp[i-1][j-1] + dp[i][j-1])
;
continue;
}
int alt1 = dp[i][j-2] + dp[i-2][j-1] + dp[i-1][j];
int alt2 = dp[i-1][j-2] + dp[i][j-1] + dp[i-2][j];
dp[i][j] = std::min(alt0, std::min(alt1, alt2));
}
}
}
return dp[len][len];
}
t**8
发帖数: 4527
9
very easy to confirm

a,b (a < b)
c, d (c < d)
1. enumerate a< c, ...... a > c .....
2 . more simple solution
pointX (a, b), pointY (c, d)
because 0 < a < b and 0 < c < d
pointX and pointY is in range less than 45 degree from the vertical
ASIX, mirror PointY to the 45 scope line. etc. compare
two distance |a -c| + |b -d| pretty easy

【在 p**********e 的大作中提到】
: 怎么证明这种解法就是最小呢
x*****0
发帖数: 452
10
m
h*****u
发帖数: 109
11
这是经典的assignment problem.
最早的算法是Kuhn提出的Hungarian algorithm.
也可以用Linear programming来formulate,所以Simplex, Dual Simplex也可以。
最快的方法是Jonker and Volgent的算法,用大量的preprocessing.
从这开始: http://en.wikipedia.org/wiki/Hungarian_algorithm
x****g
发帖数: 1512
12
为啥不是
2-3 = -1
4-6 = -2
5-1 = +4
pair不分顺序都是a-b,不能b-a?
min = 1 ?

have

【在 p**********e 的大作中提到】
: there are two groups of people; n number of A's & n of B's. We want to have
: n pairs of A and B from two groups and to minimize the sum of the weight
: differences.
: EG
: A[]={5,3,4};
: B[]={1,6,2};
: min = 5
: 3-1, 4-2,5-6

1 (共1页)
进入JobHunting版参与讨论
相关主题
近来比较重复的问题, 求解上一道我以前喜欢出的题目吧
有人做facebook的first or last这道题吗?F面经
Two problems about Algorithm问一道前几天在版上看见的题
问一个面试题大整数相乘谁贴个bug free的code
贡献一道面试题目:找出在一个数组里面只出现1次的两个数字今天竟然把MULTIPLY(大数相乘)写完了,发帖庆祝
一道要求常数空间和O(n)时间的排序题如何求一个整数阶乘的各位数字和
L家phone面,悲剧来发个我的Leetcode的Python答案吧
大牛给个大数(+-*)的面试解答吧发一个Startup的面经 - Affirm
相关话题的讨论汇总
话题: dp话题: int话题: len话题: num1话题: num2