|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <openset.wang@gmail.com> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](../course-schedule-iv "Course Schedule IV") |
| 9 | + |
| 10 | +[Next >](../maximum-product-of-two-elements-in-an-array "Maximum Product of Two Elements in an Array") |
| 11 | + |
| 12 | +## [1463. Cherry Pickup II (Hard)](https://leetcode.com/problems/cherry-pickup-ii "摘樱桃 II") |
| 13 | + |
| 14 | +<p>Given a <code>rows x cols</code> matrix <code>grid</code> representing a field of cherries. Each cell in <code>grid</code> represents the number of cherries that you can collect.</p> |
| 15 | + |
| 16 | +<p>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.</p> |
| 17 | + |
| 18 | +<p>Return the maximum number of cherries collection using both robots by following the rules below:</p> |
| 19 | + |
| 20 | +<ul> |
| 21 | + <li>From a cell (i,j), robots can move to cell (i+1, j-1) , (i+1, j) or (i+1, j+1).</li> |
| 22 | + <li>When any robot is passing through a cell, It picks it up all cherries, and the cell becomes an empty cell (0).</li> |
| 23 | + <li>When both robots stay on the same cell, only one of them takes the cherries.</li> |
| 24 | + <li>Both robots cannot move outside of the grid at any moment.</li> |
| 25 | + <li>Both robots should reach the bottom row in the <code>grid</code>.</li> |
| 26 | +</ul> |
| 27 | + |
| 28 | +<p> </p> |
| 29 | +<p><strong>Example 1:</strong></p> |
| 30 | + |
| 31 | +<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/04/29/sample_1_1802.png" style="width: 139px; height: 182px;" /></strong></p> |
| 32 | + |
| 33 | +<pre> |
| 34 | +<strong>Input:</strong> grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]] |
| 35 | +<strong>Output:</strong> 24 |
| 36 | +<strong>Explanation:</strong> Path of robot #1 and #2 are described in color green and blue respectively. |
| 37 | +Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12. |
| 38 | +Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12. |
| 39 | +Total of cherries: 12 + 12 = 24. |
| 40 | +</pre> |
| 41 | + |
| 42 | +<p><strong>Example 2:</strong></p> |
| 43 | + |
| 44 | +<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/04/23/sample_2_1802.png" style="width: 284px; height: 257px;" /></strong></p> |
| 45 | + |
| 46 | +<pre> |
| 47 | +<strong>Input:</strong> 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]] |
| 48 | +<strong>Output:</strong> 28 |
| 49 | +<strong>Explanation:</strong> Path of robot #1 and #2 are described in color green and blue respectively. |
| 50 | +Cherries taken by Robot #1, (1 + 9 + 5 + 2) = 17. |
| 51 | +Cherries taken by Robot #2, (1 + 3 + 4 + 3) = 11. |
| 52 | +Total of cherries: 17 + 11 = 28. |
| 53 | +</pre> |
| 54 | + |
| 55 | +<p><strong>Example 3:</strong></p> |
| 56 | + |
| 57 | +<pre> |
| 58 | +<strong>Input:</strong> grid = [[1,0,0,3],[0,0,0,3],[0,0,3,3],[9,0,3,3]] |
| 59 | +<strong>Output:</strong> 22 |
| 60 | +</pre> |
| 61 | + |
| 62 | +<p><strong>Example 4:</strong></p> |
| 63 | + |
| 64 | +<pre> |
| 65 | +<strong>Input:</strong> grid = [[1,1],[1,1]] |
| 66 | +<strong>Output:</strong> 4 |
| 67 | +</pre> |
| 68 | + |
| 69 | +<p> </p> |
| 70 | +<p><strong>Constraints:</strong></p> |
| 71 | + |
| 72 | +<ul> |
| 73 | + <li><code>rows == grid.length</code></li> |
| 74 | + <li><code>cols == grid[i].length</code></li> |
| 75 | + <li><code>2 <= rows, cols <= 70</code></li> |
| 76 | + <li><code>0 <= grid[i][j] <= 100 </code></li> |
| 77 | +</ul> |
| 78 | + |
| 79 | +### Related Topics |
| 80 | + [[Dynamic Programming](../../tag/dynamic-programming/README.md)] |
| 81 | + |
| 82 | +### Hints |
| 83 | +<details> |
| 84 | +<summary>Hint 1</summary> |
| 85 | +Use dynammic programming, define DP[i][j][k]: The maximum cherries that both robots can take starting on the ith row, and column j and k of Robot 1 and 2 respectively. |
| 86 | +</details> |
0 commit comments