Hi, I'm strugglin with the follow problem; Given a N * M rectangle with nums between -500 — 500, return the values of the longest sum of cells from cell (0, 0) to cell (N-1, M-1), you can make the moves (i + 1, j), (i, j + 1) and (i, j — 1), you can only visit a cell one time.
I tried the DP strategy:
//dp[i][j] is the longest sum path until the cell (i, j)
//maze[i][j] value in the cell (i, j)
dp[i][j] = max(maze[i][j], max(dp[i - 1][j] + maze[i][j], max(dp[i][j - 1] + maze[i][j], dp[i][j + 1] + maze[i][j])));
this strategy isn't working due to the test case:
81 28 240 10
40 10 100 240
20 180 110 35
the answer is: 1094
this strategy returns: 724
.
what is the optimal strategy ? Thanks.
An obviously cubic solution:
The answer will be in dp[n - 1][m - 1].
Thanks, your approach is clever than mine, but it yet results
724
on this test case I've posted before...Try to transpose matrix (or simply change order of indexes and their bounds).
Your strategy is solving problem with different set of moves.
It's problem from Timus Online Judge again :) Timus is offline now, so this is Google cache link: 1029 — Ministry.
One difference is that you should find the minimal path, not maximal.
Minimal path can be solved with more optimal (and obvious :) dijkstra in n2logn.
Yes, I've started with the Dijsktra idea, but it doesn't work for 'Longest-Paths', so I moved to the dp solution, which isn't working till now..
It still can be solved in O(n*m) using graphs.
Let's split each cell into three vertices:
0 if we came using i+1 and can move in any allowed direction;
1 if we came using j-1 and unable to move back(increasing j);
2 if we came using j+1 and unable to move back(decreasing j).
So there would be such edges(of course if they do not go out of the field):
such graph is acyclic so simple DFS/BFS will solve problem.
We can solve it in O(n*m) time. We can use DP with 3 parameters — current row, current column, and another parameter, which describes the direction we used to get on our current cell.
UPD: Oh, there was a silly bug in previous implementation (I forgot that the matrix can contain negative numbers). So, I believe that the right code is in Rev.5
What do you mean with 'In Rev.5' ?
I mean that the code is in revision 5 of my last post. If you can't find it, here it is
I hope it is true solution:
where sum[i][j] is sum of i'th row from 1 to j inclusive and dp[i][j] is the values of the longest sum of cells from cell (1, 1) to cell (i, j).
Answer will be in dp[n][m].
This problem is trickier than I thought, this soluition looks correct, but In the test case I posted on previously, it returns 395. weird.
for this test case ??? my comp returns 1094 !
I'm sorry, your solution get TLE, ivan100sic solution is the one which get wrong answer... I'm really lost now, I think the solution must be O(N^2), but I can't think in a faster solution, they both looks right, I can't think the test case ivan100sic fails also.
What about Fcdkbear's solution ???
This solution gets WA... I really can't think in other possible states for this problem.
What about my idea? Did you tested it?
I think, our ideas are almost identical. The only difference is that I don't build graph physically.
Can you give a link to the problem? For example, constant -1000000000 depends on sizes of the rectangle.
Link I'm seeing your idea now, I'm confused in the way you are building the edges.
Got AC:) It was not so easy, because I don't know Portugal language and this problem has strange time limit(1-2 seconds, as it was mentioned in the problem statement :))
There was a bug in my code(in one place we should use y>0, not y>=0). So here is the link to my code.
Oh, thank you, I'm analyzing your code right now, as I'm newbie in this DP in recurrence relations with memorization I'll take a while do understand everything, thank you. if you know problems like these (with memorization with recursion solution) I'd appreciate if you send me, once again, thanks.
For example, a lot of this problems can be solved using recursion with memorization.
I think I have a linear time solution for this one, i'll post it soon.
There you go, give it a spin:
The solution can be found in
Dp[N+1][M]
I used the (1,1)..(N,M) convention.Dp[i][j]
stores the maximum value of a valid path from (1,1)..(i,j) which enters the field (i,j) from above, that is, from (i-1,j).Full implementation: Here