Overview
Instead of finding a path down and a path up, we imagine two agents starting at (0,0) and walking to (N-1,N-1) simultaneously.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Collect maximum cherries going from (0,0) to (N-1,N-1) and back. We can model this as two people starting at (0,0) simultaneously.
Instead of finding a path down and a path up, we imagine two agents starting at (0,0) and walking to (N-1,N-1) simultaneously.
Since they move at the same speed, after `t` steps, `r1 + c1 = r2 + c2 = t`. Thus, we only need to track `r1` and `r2` for the current step `t`.
From the previous step `t-1`, there are 4 possible moves to reach `(r1, c1)` and `(r2, c2)`: (Right, Right), (Down, Right), (Right, Down), (Down, Down).
If `r1 == r2` (and thus `c1 == c2`), they are on the same cell, so we only add the cherry once. Otherwise, we add cherries from both cells.