Redian新闻
>
I will buy back USD at 1.027 today with CAD
avatar
I will buy back USD at 1.027 today with CAD# Stock
d*v
1
一个m*n的matrix每个格子上放一定数量的钱,机器人走过一个格子就把钱拿走,从左
上角走到右下角,只能向下向右走。求出两个机器人能拿到的钱的总和最大值。
avatar
k*n
2
good luck all!
avatar
u*a
3
两个机器人?

【在 d******v 的大作中提到】
: 一个m*n的matrix每个格子上放一定数量的钱,机器人走过一个格子就把钱拿走,从左
: 上角走到右下角,只能向下向右走。求出两个机器人能拿到的钱的总和最大值。

avatar
d*v
4
是啊,一个机器人就是leetcode原题了。

【在 u*a 的大作中提到】
: 两个机器人?
avatar
u*a
5
两个从同一个角出发么?走一遍把走过地方设为0,再走一遍不就完了。

【在 d******v 的大作中提到】
: 是啊,一个机器人就是leetcode原题了。
avatar
w*k
6
先求得最大值和路径,然后把路径上的单元都设为0,然后再来一遍,相加即可。有反
例么?
avatar
h*t
7
Counter example
0, 0, 2
0, 3, 2
0, 1, 0
greedy = (3 + 2) + 2 = 7
optimal = (3 + 1) + (2 + 2) = 8
avatar
l*a
8
分成上下三角分开走?
avatar
r*7
9
看成一个图,把两个机器人看成机器人从左上走到右下,再从右下走到左上这么一个路
径,找到最长的路径。

【在 d******v 的大作中提到】
: 一个m*n的matrix每个格子上放一定数量的钱,机器人走过一个格子就把钱拿走,从左
: 上角走到右下角,只能向下向右走。求出两个机器人能拿到的钱的总和最大值。

avatar
u*a
10
不能直接找吧。这不是一个静态的图,走过的格子被清零了。

【在 r****7 的大作中提到】
: 看成一个图,把两个机器人看成机器人从左上走到右下,再从右下走到左上这么一个路
: 径,找到最长的路径。

avatar
l*1
11
3维DP
avatar
r*7
12
清不清零没影响,反正你也不能走同一个格子两次

【在 u*a 的大作中提到】
: 不能直接找吧。这不是一个静态的图,走过的格子被清零了。
avatar
u*a
13
Why not?

【在 r****7 的大作中提到】
: 清不清零没影响,反正你也不能走同一个格子两次
avatar
i*e
14
钱的数量改成负数,然后求流量为2的最小费用流。再取负就是答案。
avatar
i*e
15
话说回来,这是某人/某书上的变体还是真的有人面试时被问了?面试问费用流也算是
变态了。
avatar
h*t
16
The cost of minimum-cost flow problem is on the edges, not on the vertices.
avatar
d*v
17
这是原来有人发过的g家面经

【在 i*********e 的大作中提到】
: 话说回来,这是某人/某书上的变体还是真的有人面试时被问了?面试问费用流也算是
: 变态了。

avatar
i*e
18

不好意思,我略过了一些步骤。
把每个vertice分拆成两个vertice, create two edges between them, one with INF
capacity and 0 cost, one with capacity of 1 and cost of -money. Edges
between the original vertices stays the same, with 0 cost and INF capacity(
or capacity of 2, does not matter).
This is the standard way to move cost/capacity to edges.

【在 h****t 的大作中提到】
: The cost of minimum-cost flow problem is on the edges, not on the vertices.
avatar
h*t
19
多谢,受教了。这个面试题未免太难了点。
avatar
U*A
20
有没有人给个算法。
avatar
f*s
21
If there is one robot, it can be at each of m*n locations, with DP,
calculation needs to be done once only for each location so the time
complexity is O(m*n).
If there are two robots, they can be at each of (m*n)*(m*n) locations, again
with DP, calculation needs to be done once only for each location so the
time complexity is O(m*n*m*n).
avatar
U*A
22
给个具体算法?

again

【在 f*********s 的大作中提到】
: If there is one robot, it can be at each of m*n locations, with DP,
: calculation needs to be done once only for each location so the time
: complexity is O(m*n).
: If there are two robots, they can be at each of (m*n)*(m*n) locations, again
: with DP, calculation needs to be done once only for each location so the
: time complexity is O(m*n*m*n).

avatar
j*g
23
int twoRobs(vector > &MP){
int M = MP.size();
int N = MP[0].size();
int P_LEN = M + N -1;
vector > dp1 = vector >(N, vector(N, 0));
vector > dp2 = vector >(N, vector(N, 0));
dp1[0][0] = MP[0][0];
dp2[0][0] = MP[0][0];
for (int p = 2; p <= P_LEN; p++){//p is the length of the path
for (int x1 = 0; x1 < N; x1++){
for (int x2 = x1; x2 < N; x2++){
int y1 = p - 1 - x1;
int y2 = p - 1 - x2;
if(y1<0 || y2<0 || y1>=M || y2>=M)
continue;
int best = 0;
int delta = MP[y1][x1];
if (x1 != x2)
delta += MP[y2][x2];
if(x1>0 && x2>0)//from left left
best = max(best, dp1[x1-1][x2-1] + delta);
if(x1>0 && y2>0)//from left up
best = max(best, dp1[x1-1][x2] + delta);
if(y1>0 && x2>0)//from up left
best = max(best, dp1[x1][x2-1] + delta);
if(y1>0 && y2>0)//from up up
best = max(best, dp1[x1][x2] + delta);
dp2[x1][x2] = best;
}
}
dp1=dp2;
}
return dp1[N-1][N-1];
}
avatar
W*y
24
传纸条模型
三维dp

【在 d******v 的大作中提到】
: 一个m*n的matrix每个格子上放一定数量的钱,机器人走过一个格子就把钱拿走,从左
: 上角走到右下角,只能向下向右走。求出两个机器人能拿到的钱的总和最大值。

avatar
d*8
25
这就是一个二维动态规划的问题.
下面是我的代码链接和用 hlight 给的例子作为测试来验证的代码:
http://ideone.com/fnC3de
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。