Hard
Quiz
#1463 Cherry Pickup II
APPROACH
A rows x cols matrix grid describes a cherry field where grid[i][j] is the cherry count at cell (i, j). Two robots start at opposite corners of the top row: Robot 1 at (0, 0) and Robot 2 at (0, cols-1).
Both robots must reach the bottom row, moving one step down each turn to a diagonally or directly adjacent cell (left-down, straight down, or right-down). When both visit the same cell, only one collects the cherries. Neither robot may move outside the grid.
Return the maximum total cherries both robots can collect.
Example 1:
Input: grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]]
Output: 24
Explanation: Path of robot #1 and #2 are described in color green and blue respectively.
Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12.
Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12.
Total of cherries: 12 + 12 = 24.
Example 2:
Input: grid = [[1,0,0,0,0,0,1],[2,0,0,0,0,3,0],[2,0,9,0,0,0,0],[0,3,0,5,4,0,0],[1,0,2,3,0,0,6]]
Output: 28
Explanation: Path of robot #1 and #2 are described in color green and blue respectively.
Cherries taken by Robot #1, (1 + 9 + 5 + 2) = 17.
Cherries taken by Robot #2, (1 + 3 + 4 + 3) = 11.
Total of cherries: 17 + 11 = 28.
1 of 4
1:00
What is the optimal approach for this problem?