## 741. Cherry Pickup

In a N x N `grid` representing a field of cherries, each cell is one of three possible integers.

• 0 means the cell is empty, so you can pass through;
• 1 means the cell contains a cherry, that you can pick up and pass through;
• -1 means the cell contains a thorn that blocks your way.
• Your task is to collect maximum number of cherries possible by following the rules below:

• Starting at the position (0, 0) and reaching (N-1, N-1) by moving right or down through valid path cells (cells with value 0 or 1);
• After reaching (N-1, N-1), returning to (0, 0) by moving left or up through valid path cells;
• When passing through a path cell containing a cherry, you pick it up and the cell becomes an empty cell (0);
• If there is no valid path between (0, 0) and (N-1, N-1), then no cherries can be collected.
• Example 1:

```Input: grid =
[[0, 1, -1],
[1, 0, -1],
[1, 1,  1]]
Output: 5
Explanation:
The player started at (0, 0) and went down, down, right right to reach (2, 2).
4 cherries were picked up during this single trip, and the matrix becomes [[0,1,-1],[0,0,-1],[0,0,0]].
Then, the player went left, up, up, left to return home, picking up one more cherry.
The total number of cherries picked up is 5, and this is the maximum possible.
```

Note:

• `grid` is an `N` by `N` 2D array, with `1 <= N <= 50`.
• Each `grid[i][j]` is an integer in the set `{-1, 0, 1}`.
• It is guaranteed that grid and grid[N-1][N-1] are not -1.

• b'
\n\n

#### Approach #1: Greedy [Wrong Answer]

\n

Intuition

\n

Let\'s find the most cherries we can pick up with one path, pick them up, then find the most cherries we can pick up with a second path on the remaining field.

\n

Though a counter example might be hard to think of, this approach fails to find the best answer to this case:

\n
`11100\n00101\n10100\n00100\n00111\n`
\n

Algorithm

\n

We can use dynamic programming to find the most number of cherries `dp[i][j]` that can be picked up from any location `(i, j)` to the bottom right corner. This is a classic question very similar to Minimum Path Sum, refer to the link if you are not familiar with this type of question.

\n

After, we can find an first path that maximizes the number of cherries taken by using our completed `dp` as an oracle for deciding where to move. We\'ll choose the move that allows us to pick up more cherries (based on comparing `dp[i+1][j]` and `dp[i][j+1]`).

\n

After taking the cherries from that path (and removing it from the grid), we\'ll take the cherries again.

\n\n

Complexity Analysis

\n
\n
• \n

Time Complexity: , where is the length of `grid`. Our dynamic programming consists of two for-loops of length `N`.

\n
• \n
• \n

Space Complexity: , the size of `dp`.

\n
• \n
\n
\n

#### Approach #2: Dynamic Programming (Top Down) [Accepted]

\n

Intuition

\n

Instead of walking from end to beginning, let\'s reverse the second leg of the path, so we are only considering two paths from the beginning to the end.

\n

Notice after `t` steps, each position `(r, c)` we could be, is on the line `r + c = t`. So if we have two people at positions `(r1, c1)` and `(r2, c2)`, then `r2 = r1 + c1 - c2`. That means the variables `r1, c1, c2` uniquely determine 2 people who have walked the same `r1 + c1` number of steps. This sets us up for dynamic programming quite nicely.

\n

Algorithm

\n

Let `dp[r1][c1][c2]` be the most number of cherries obtained by two people starting at `(r1, c1)` and `(r2, c2)` and walking towards `(N-1, N-1)` picking up cherries, where `r2 = r1+c1-c2`.

\n

If `grid[r1][c1]` and `grid[r2][c2]` are not thorns, then the value of `dp[r1][c1][c2]` is `(grid[r1][c1] + grid[r2][c2])`, plus the maximum of `dp[r1+1][c1][c2]`, `dp[r1][c1+1][c2]`, `dp[r1+1][c1][c2+1]`, `dp[r1][c1+1][c2+1]` as appropriate. We should also be careful to not double count in case `(r1, c1) == (r2, c2)`.

\n\n

Complexity Analysis

\n
\n
• \n

Time Complexity: , where is the length of `grid`. Our dynamic programming has states.

\n
• \n
• \n

Space Complexity: , the size of `memo`.

\n
• \n
\n
\n

#### Approach #3: Dynamic Programming (Bottom Up) [Accepted]

\n

Intuition

\n

Like in Approach #2, we have the idea of dynamic programming.

\n

Say `r1 + c1 = t` is the `t`-th layer. Since our recursion only references the next layer, we only need to keep two layers in memory at a time.

\n

Algorithm

\n

At time `t`, let `dp[c1][c2]` be the most cherries that we can pick up for two people going from `(0, 0)` to `(r1, c1)` and `(0, 0)` to `(r2, c2)`, where `r1 = t-c1, r2 = t-c2`. Our dynamic program proceeds similarly to Approach #2.

\n\n

Complexity Analysis

\n
\n
• \n

Time Complexity: , where is the length of `grid`. We have three for-loops of size .

\n
• \n
• \n

Space Complexity: , the sizes of `dp` and `dp2`.

\n
• \n
\n
\n

Analysis written by: @awice. Solution 3 inspired by @uwi.

\n
'