You are given an n x n grid representing a field of cherries, each cell is one of three possible integers.
0 means the cell is empty, so you can pass through,1 means the cell contains a cherry that you can pick up and pass through, or-1 means the cell contains a thorn that blocks your way.Return the maximum number of cherries you can collect by following the rules below:
(0, 0) and reaching (n - 1, n - 1) by moving right or down through valid path cells (cells with value 0 or 1).(n - 1, n - 1), returning to (0, 0) by moving left or up through valid path cells.0.(0, 0) and (n - 1, n - 1), then no cherries can be collected.
Example 1:
Input: grid = [[0,1,-1],[1,0,-1],[1,1,1]] Output: 5 Explanation: The player started at (0, 0) and went down, down, right right to reach (2, 2). 4 cherries were picked up during this single trip, and the matrix becomes [[0,1,-1],[0,0,-1],[0,0,0]]. Then, the player went left, up, up, left to return home, picking up one more cherry. The total number of cherries picked up is 5, and this is the maximum possible.
Example 2:
Input: grid = [[1,1,-1],[1,-1,1],[-1,1,1]] Output: 0
Constraints:
n == grid.lengthn == grid[i].length1 <= n <= 50grid[i][j] is -1, 0, or 1.grid[0][0] != -1grid[n - 1][n - 1] != -1We will approach the problem using dynamic programming by maintaining a 3D DP table. The idea is to simulate two individuals traversing the grid simultaneously: one moving from (0,0) to (n-1,n-1) and the other returning from (n-1,n-1) back to (0,0). Both will walk through cells to collect cherries with the understanding that a cell’s cherry can be collected only once.
The DP table, dp[x1][y1][x2], represents the maximum cherries collected when person 1 is at (x1,y1) and person 2 is at (x2,y1+x1-x2) such that both paths have used same number of steps. Transitions are explored based on valid grid movements (right or down) and allowed combinations.
This method ensures we utilize optimal substructure and memoization to avoid computing the same state multiple times.
The implemented solution uses a 3D DP array where each state is represented by the positions of two players (x1, y1, x2) which directly determines (x2, y2) due to the step count equality of x1+y1 and x2+y2. Each entry fills based on maximum cherries collectable given their current step scenario, striving to improve from all possible move origins (right or down).
Java
C++
Time Complexity: O(n^3), due to iterating through each position combination for both players.
Space Complexity: O(n^3), the space needed to store results of all position combinations.
Given the constraints of the initial approach, further space optimization can be applied by minimizing the dimensions that are updated in the DP table at any given time. We'll reduce our 3D DP array into a 2D array considering only necessary state information, as they relate to specific grid traversals. This lowering of space dimensions capitalizes on the symmetrical nature of path reversals without sacrificing calculation correctness or granularity.
This solution keeps updating the 2D DP table along each step of a journey, maintaining only pertinent states per each t (sum of (x1 + y1)) layer. This slimmed-down model reduces memory burden significantly by recycling layers in place of ever-expanding a 3D matrix, aligning closely with each movement's iterative aspect.
Time Complexity: O(n^3), iterating through elements optimized per journey line level.
Space Complexity: O(n^2), condensing memos to one comprehensive 2D table pairing.
| Approach | Complexity |
|---|---|
| Dynamic Programming with 3D DP Table | Time Complexity: O(n^3), due to iterating through each position combination for both players. Space Complexity: O(n^3), the space needed to store results of all position combinations. |
| Dynamic Programming with Space Optimization | Time Complexity: O(n^3), iterating through elements optimized per journey line level. Space Complexity: O(n^2), condensing memos to one comprehensive 2D table pairing. |
DP 13. Cherry Pickup II | 3D DP Made Easy | DP On Grids • take U forward • 280,890 views views
Watch 9 more video solutions →Practice Cherry Pickup with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor