Monday, August 31, 2020

LeetCode [1463] Cherry Pickup II

 1463. Cherry Pickup II

Hard

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.

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.

Return the maximum number of cherries collection using both robots  by following the rules below:

  • From a cell (i,j), robots can move to cell (i+1, j-1) , (i+1, j) or (i+1, j+1).
  • When any robot is passing through a cell, It picks it up all cherries, and the cell becomes an empty cell (0).
  • When both robots stay on the same cell, only one of them takes the cherries.
  • Both robots cannot move outside of the grid at any moment.
  • Both robots should reach the bottom row in the grid.

 

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.

Example 3:

Input: grid = [[1,0,0,3],[0,0,0,3],[0,0,3,3],[9,0,3,3]]
Output: 22

Example 4:

Input: grid = [[1,1],[1,1]]
Output: 4

 

Constraints:

  • rows == grid.length
  • cols == grid[i].length
  • 2 <= rows, cols <= 70
  • 0 <= grid[i][j] <= 100 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
- 3D-DP question
- m: rows. n: columns
- Must move down at every step, hence there are m-1 steps
- dp is n*n matrix.
- Update dp at every step k=1..m-1
- At step k, dp[i][j] maintains the max cherries when robot1 is at [k,i] and robot2 is at [k,j]
*/
class Solution {
    int[] shift = new int[]{-1,0,1};
    
    public int cherryPickup(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        int[][] dp = new int[n][n];
        for(int t=0; t<n; ++t) Arrays.fill(dp[t], -1);
        dp[0][n-1] = grid[0][0] + grid[0][n-1];
 
        int ret = 0;
        for(int k=1; k<=m-1; ++k){
            int[][] tmp = new int[n][n];
            for(int t=0; t<n; ++t) Arrays.fill(tmp[t], -1);
            for(int i=0; i<n; ++i){
                for(int j=0; j<n; ++j){
                    int cherries = -1;
                    for(int di : shift){
                        for(int dj : shift){
                            if(i+di>=0 && i+di<n && j+dj>=0 && j+dj<n){
                                cherries = Math.max(cherries, dp[i+di][j+dj]);
                            }
                        }
                    }
                    if(cherries<0) continue;
                    tmp[i][j] = cherries + grid[k][i] + (j==i?0:grid[k][j]);
                    ret = Math.max(ret, tmp[i][j]);
                }
            }
            dp = tmp;
        }
        
        return ret;
    }
}

No comments:

Post a Comment