Class Solution

java.lang.Object
g1401_1500.s1463_cherry_pickup_ii.Solution

public class Solution extends Object
1463 - Cherry Pickup II.<p>Hard</p> <p>You are given a <code>rows x cols</code> matrix <code>grid</code> representing a field of cherries where <code>grid[i][j]</code> represents the number of cherries that you can collect from the <code>(i, j)</code> cell.</p> <p>You have two robots that can collect cherries for you:</p> <ul> <li><strong>Robot #1</strong> is located at the <strong>top-left corner</strong> <code>(0, 0)</code>, and</li> <li><strong>Robot #2</strong> is located at the <strong>top-right corner</strong> <code>(0, cols - 1)</code>.</li> </ul> <p>Return <em>the maximum number of cherries collection using both robots by following the rules below</em>:</p> <ul> <li>From a cell <code>(i, j)</code>, robots can move to cell <code>(i + 1, j - 1)</code>, <code>(i + 1, j)</code>, or <code>(i + 1, j + 1)</code>.</li> <li>When any robot passes through a cell, It picks up all cherries, and the cell becomes an empty cell.</li> <li>When both robots stay in the same cell, only one takes the cherries.</li> <li>Both robots cannot move outside of the grid at any moment.</li> <li>Both robots should reach the bottom row in <code>grid</code>.</li> </ul> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/04/29/sample_1_1802.png" alt="" /></p> <p><strong>Input:</strong> grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]]</p> <p><strong>Output:</strong> 24</p> <p><strong>Explanation:</strong> Path of robot #1 and #2 are described in color green and blue respectively.</p> <p>Cherries taken by Robot #1, (3 + 2 + 5 + 2) = 12.</p> <p>Cherries taken by Robot #2, (1 + 5 + 5 + 1) = 12.</p> <p>Total of cherries: 12 + 12 = 24.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/04/23/sample_2_1802.png" alt="" /></p> <p><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]]</p> <p><strong>Output:</strong> 28</p> <p><strong>Explanation:</strong> Path of robot #1 and #2 are described in color green and blue respectively.</p> <p>Cherries taken by Robot #1, (1 + 9 + 5 + 2) = 17.</p> <p>Cherries taken by Robot #2, (1 + 3 + 4 + 3) = 11.</p> <p>Total of cherries: 17 + 11 = 28.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>rows == grid.length</code></li> <li><code>cols == grid[i].length</code></li> <li><code>2 <= rows, cols <= 70</code></li> <li><code>0 <= grid[i][j] <= 100</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • cherryPickup

      public int cherryPickup(int[][] grid)