|
| 1 | +# Cherry Pickup II |
| 2 | + |
| 3 | +## Difficulty |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## Problem: |
| 8 | + |
| 9 | +Given a rows x cols matrix grid representing a field of cherries. Each cell in grid represents the number of cherries that you can collect. |
| 10 | + |
| 11 | +You have two robots that can collect cherries for you, Robot #1 is located at the top-left corner (0,0) , and Robot #2 is located at the top-right corner (0, cols-1) of the grid. |
| 12 | + |
| 13 | +Return the maximum number of cherries collection using both robots by following the rules below: |
| 14 | + |
| 15 | +- From a cell (i,j), robots can move to cell (i+1, j-1) , (i+1, j) or (i+1, j+1). |
| 16 | +- When any robot is passing through a cell, It picks it up all cherries, and the cell becomes an empty cell (0). |
| 17 | +- When both robots stay on the same cell, only one of them takes the cherries. |
| 18 | +- Both robots cannot move outside of the grid at any moment. |
| 19 | +- Both robots should reach the bottom row in the grid. |
| 20 | + |
| 21 | +<!-- any examples --> |
| 22 | + |
| 23 | +### Example 1: |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +``` |
| 28 | +Input: grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]] |
| 29 | +Output: 24 |
| 30 | +Explanation: Path of robot #1 and #2 are described in color green and blue respectively. |
| 31 | +Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12. |
| 32 | +Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12. |
| 33 | +Total of cherries: 12 + 12 = 24. |
| 34 | +``` |
| 35 | + |
| 36 | +### Example 2: |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +``` |
| 41 | +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]] |
| 42 | +Output: 28 |
| 43 | +Explanation: Path of robot #1 and #2 are described in color green and blue respectively. |
| 44 | +Cherries taken by Robot #1, (1 + 9 + 5 + 2) = 17. |
| 45 | +Cherries taken by Robot #2, (1 + 3 + 4 + 3) = 11. |
| 46 | +Total of cherries: 17 + 11 = 28. |
| 47 | +``` |
| 48 | + |
| 49 | +### Example 4: |
| 50 | + |
| 51 | +``` |
| 52 | +Input: grid = [[1,0,0,3],[0,0,0,3],[0,0,3,3],[9,0,3,3]] |
| 53 | +Output: 22 |
| 54 | +``` |
| 55 | + |
| 56 | +### Example 5: |
| 57 | + |
| 58 | +``` |
| 59 | +Input: grid = [[1,1],[1,1]] |
| 60 | +Output: 4 |
| 61 | +``` |
| 62 | + |
| 63 | +### Constraints |
| 64 | + |
| 65 | +`rows == grid.length` |
| 66 | + |
| 67 | +`cols == grid[i].length` |
| 68 | + |
| 69 | +`2 <= rows, cols <= 70` |
| 70 | + |
| 71 | +`0 <= grid[i][j] <= 100 ` |
| 72 | + |
| 73 | +<details> |
| 74 | + <summary>Solutions (Click to expand)</summary> |
| 75 | + |
| 76 | +### Explanation |
| 77 | + |
| 78 | +<!-- solution explanation --> |
| 79 | + |
| 80 | +Since each robot can only one move down grid's height `h` each time it collects cherries then the problem be simplified as finding the best possible movements the robots can make at each level. |
| 81 | + |
| 82 | +The possible movements of each robot are straight down `i` down-left `i-1` and down-right `i+1`. There are a possible 9 different combinations of movements the robots can make for each level from each movement. This amounts to 9^h possible movement that can be made. We can use dfs function that recursively traverses the each one of these possible moves to until the bottom, calculates the cherries collected for each move, takes the max out of all three, and adds it to each move of the the upper level to repeat the process. |
| 83 | + |
| 84 | +Since that are a total of 9^h operations need to completely traverse the grid, we need a way to reduce the amount of possible recalculations and recursive that happen when traversing previously familiar paths. In order to reduce the total number of operations we can use memoization to save max number of cherries that can collected from the bottom to the cell (bottom up dp). This will short-circuit recursive calls and dramatically reduce the number of operations need to traverse the entire grid. Since the robots can only move down 1 cell for each movement we can use a 3d array with of size `h * i * j` |
| 85 | + |
| 86 | +<!-- relative links to solution files. {title} should be replaced with the name of the problem in `kebab-case` --> |
| 87 | + |
| 88 | +- [JavaScript](./cherry-pickup-ii.js) |
| 89 | +- [TypeScript](./cherry-pickup-ii.ts) |
| 90 | +- [Java](./cherry-pickup-ii.java) |
| 91 | +- [Go](./cherry-pickup-ii.go) |
| 92 | +</details> |
0 commit comments