There are 3 main approaches to solve this problem:
The key insight for solving this problem is to reframe it. Instead of thinking about one person making a round trip, we can think of it as two people starting from (0, 0)
and both reaching (n-1, n-1)
.
At each step, both people can move either right or down, and we need to maximize the total number of cherries they collect. If both people are at the same cell, we should count the cherry only once.
Let's define dp[r1][c1][r2][c2]
as the maximum cherries that can be collected when person 1 is at (r1, c1)
and person 2 is at (r2, c2)
.
The recurrence relation is:
dp[r1][c1][r2][c2] = grid[r1][c1] + grid[r2][c2] - (r1 == r2 && c1 == c2 ? grid[r1][c1] : 0) + max(dp[r1-1][c1][r2-1][c2], dp[r1-1][c1][r2][c2-1], dp[r1][c1-1][r2-1][c2], dp[r1][c1-1][r2][c2-1])
The base case is dp[0][0][0][0] = grid[0][0]
.
However, there's a key observation we can make: if both people take the same number of steps, then r1 + c1 = r2 + c2
. This means we can reduce the 4D DP array to a 3D DP array dp[r1][c1][r2]
, where c2 = r1 + c1 - r2
.
The recurrence relation becomes:
dp[r1][c1][r2] = grid[r1][c1] + (r1 == r2 && c1 == r1 + c1 - r2 ? 0 : grid[r2][r1 + c1 - r2]) + max(dp[r1-1][c1][r2-1], dp[r1-1][c1][r2], dp[r1][c1-1][r2-1], dp[r1][c1-1][r2])
We need to be careful about the boundary conditions and ensure that both people are on valid cells (not thorns).
We need to fill in the DP table of size n^4, and each cell takes constant time to compute.
We use a 4D DP array of size n^4 to store the maximum cherries for each position.
We can optimize the space complexity of the dynamic programming approach by using a 3D array instead of a 4D array.
The key observation is that if both people take the same number of steps, then r1 + c1 = r2 + c2
. This means we can reduce the 4D DP array to a 3D DP array dp[r1][c1][r2]
, where c2 = r1 + c1 - r2
.
Let's define dp[r1][c1][r2]
as the maximum cherries that can be collected when person 1 is at (r1, c1)
and person 2 is at (r2, r1 + c1 - r2)
.
The recurrence relation is:
dp[r1][c1][r2] = grid[r1][c1] + (r1 == r2 && c1 == r1 + c1 - r2 ? 0 : grid[r2][r1 + c1 - r2]) + max(dp[r1-1][c1][r2-1], dp[r1-1][c1][r2], dp[r1][c1-1][r2-1], dp[r1][c1-1][r2])
We need to be careful about the boundary conditions and ensure that both people are on valid cells (not thorns).
We need to fill in the DP table of size n^3, and each cell takes constant time to compute.
We use a 3D DP array of size n^3 to store the maximum cherries for each position.
We can also solve this problem using a top-down approach with memoization, which is essentially a recursive solution with caching to avoid redundant calculations.
Let's define a recursive function dp(r1, c1, r2, c2)
that returns the maximum cherries that can be collected when person 1 is at (r1, c1)
and person 2 is at (r2, c2)
.
The recurrence relation is the same as in the bottom-up approach:
dp(r1, c1, r2, c2) = grid[r1][c1] + grid[r2][c2] - (r1 == r2 && c1 == c2 ? grid[r1][c1] : 0) + max(dp(r1-1, c1, r2-1, c2), dp(r1-1, c1, r2, c2-1), dp(r1, c1-1, r2-1, c2), dp(r1, c1-1, r2, c2-1))
We can optimize this by using the observation that r1 + c1 = r2 + c2
, so we can define dp(r1, c1, r2)
where c2 = r1 + c1 - r2
.
To avoid redundant calculations, we use memoization to store the results of subproblems that we've already solved.
We need to compute the result for each state (r1, c1, r2), and there are n^3 possible states. Each state takes constant time to compute.
We use a memoization table of size n^3 to store the results of subproblems, and the recursion stack can go up to a depth of O(n).
123456789101112131415161718192021222324252627282930313233343536function cherryPickup(grid): n = grid.length // Initialize DP array dp = new 4D array of size (n+1) × (n+1) × (n+1) × (n+1), initialized with -Infinity // Base case dp[0][0][0][0] = grid[0][0] // Fill DP array for r1 from 0 to n-1: for c1 from 0 to n-1: for r2 from 0 to n-1: for c2 from 0 to n-1: // Skip invalid cells if grid[r1][c1] == -1 or grid[r2][c2] == -1: continue // Calculate cherries collected cherries = grid[r1][c1] if r1 != r2 or c1 != c2: cherries += grid[r2][c2] // Calculate maximum cherries from previous positions prev = max( dp[r1-1][c1][r2-1][c2] if r1 > 0 and r2 > 0 else -Infinity, dp[r1-1][c1][r2][c2-1] if r1 > 0 and c2 > 0 else -Infinity, dp[r1][c1-1][r2-1][c2] if c1 > 0 and r2 > 0 else -Infinity, dp[r1][c1-1][r2][c2-1] if c1 > 0 and c2 > 0 else -Infinity ) // Update DP array if prev != -Infinity: dp[r1][c1][r2][c2] = cherries + prev return max(0, dp[n-1][n-1][n-1][n-1])
Understand different approaches to solve the cherry pickup problem and analyze their efficiency.
The key insight for solving this problem is to reframe it. Instead of thinking about one person making a round trip, we can think of it as two people starting from (0, 0)
and both reaching (n-1, n-1)
.
At each step, both people can move either right or down, and we need to maximize the total number of cherries they collect. If both people are at the same cell, we should count the cherry only once.
Let's define dp[r1][c1][r2][c2]
as the maximum cherries that can be collected when person 1 is at (r1, c1)
and person 2 is at (r2, c2)
.
The recurrence relation is:
dp[r1][c1][r2][c2] = grid[r1][c1] + grid[r2][c2] - (r1 == r2 && c1 == c2 ? grid[r1][c1] : 0) + max(dp[r1-1][c1][r2-1][c2], dp[r1-1][c1][r2][c2-1], dp[r1][c1-1][r2-1][c2], dp[r1][c1-1][r2][c2-1])
The base case is dp[0][0][0][0] = grid[0][0]
.
However, there's a key observation we can make: if both people take the same number of steps, then r1 + c1 = r2 + c2
. This means we can reduce the 4D DP array to a 3D DP array dp[r1][c1][r2]
, where c2 = r1 + c1 - r2
.
The recurrence relation becomes:
dp[r1][c1][r2] = grid[r1][c1] + (r1 == r2 && c1 == r1 + c1 - r2 ? 0 : grid[r2][r1 + c1 - r2]) + max(dp[r1-1][c1][r2-1], dp[r1-1][c1][r2], dp[r1][c1-1][r2-1], dp[r1][c1-1][r2])
We need to be careful about the boundary conditions and ensure that both people are on valid cells (not thorns).
We can optimize the space complexity of the dynamic programming approach by using a 3D array instead of a 4D array.
The key observation is that if both people take the same number of steps, then r1 + c1 = r2 + c2
. This means we can reduce the 4D DP array to a 3D DP array dp[r1][c1][r2]
, where c2 = r1 + c1 - r2
.
Let's define dp[r1][c1][r2]
as the maximum cherries that can be collected when person 1 is at (r1, c1)
and person 2 is at (r2, r1 + c1 - r2)
.
The recurrence relation is:
dp[r1][c1][r2] = grid[r1][c1] + (r1 == r2 && c1 == r1 + c1 - r2 ? 0 : grid[r2][r1 + c1 - r2]) + max(dp[r1-1][c1][r2-1], dp[r1-1][c1][r2], dp[r1][c1-1][r2-1], dp[r1][c1-1][r2])
We need to be careful about the boundary conditions and ensure that both people are on valid cells (not thorns).
We can also solve this problem using a top-down approach with memoization, which is essentially a recursive solution with caching to avoid redundant calculations.
Let's define a recursive function dp(r1, c1, r2, c2)
that returns the maximum cherries that can be collected when person 1 is at (r1, c1)
and person 2 is at (r2, c2)
.
The recurrence relation is the same as in the bottom-up approach:
dp(r1, c1, r2, c2) = grid[r1][c1] + grid[r2][c2] - (r1 == r2 && c1 == c2 ? grid[r1][c1] : 0) + max(dp(r1-1, c1, r2-1, c2), dp(r1-1, c1, r2, c2-1), dp(r1, c1-1, r2-1, c2), dp(r1, c1-1, r2, c2-1))
We can optimize this by using the observation that r1 + c1 = r2 + c2
, so we can define dp(r1, c1, r2)
where c2 = r1 + c1 - r2
.
To avoid redundant calculations, we use memoization to store the results of subproblems that we've already solved.
We need to fill in the DP table of size n^4, and each cell takes constant time to compute.
We use a 4D DP array of size n^4 to store the maximum cherries for each position.
We need to fill in the DP table of size n^3, and each cell takes constant time to compute.
We use a 3D DP array of size n^3 to store the maximum cherries for each position.
We need to compute the result for each state (r1, c1, r2), and there are n^3 possible states. Each state takes constant time to compute.
We use a memoization table of size n^3 to store the results of subproblems, and the recursion stack can go up to a depth of O(n).
123456789101112131415161718192021222324252627282930313233343536function cherryPickup(grid): n = grid.length // Initialize DP array dp = new 4D array of size (n+1) × (n+1) × (n+1) × (n+1), initialized with -Infinity // Base case dp[0][0][0][0] = grid[0][0] // Fill DP array for r1 from 0 to n-1: for c1 from 0 to n-1: for r2 from 0 to n-1: for c2 from 0 to n-1: // Skip invalid cells if grid[r1][c1] == -1 or grid[r2][c2] == -1: continue // Calculate cherries collected cherries = grid[r1][c1] if r1 != r2 or c1 != c2: cherries += grid[r2][c2] // Calculate maximum cherries from previous positions prev = max( dp[r1-1][c1][r2-1][c2] if r1 > 0 and r2 > 0 else -Infinity, dp[r1-1][c1][r2][c2-1] if r1 > 0 and c2 > 0 else -Infinity, dp[r1][c1-1][r2-1][c2] if c1 > 0 and r2 > 0 else -Infinity, dp[r1][c1-1][r2][c2-1] if c1 > 0 and c2 > 0 else -Infinity ) // Update DP array if prev != -Infinity: dp[r1][c1][r2][c2] = cherries + prev return max(0, dp[n-1][n-1][n-1][n-1])
1234567891011121314151617181920212223242526272829303132333435363738function cherryPickup(grid): n = grid.length // Initialize DP array dp = new 3D array of size n × n × n, initialized with -Infinity // Base case dp[0][0][0] = grid[0][0] // Fill DP array for r1 from 0 to n-1: for c1 from 0 to n-1: for r2 from 0 to n-1: // Calculate c2 c2 = r1 + c1 - r2 // Skip invalid cells if c2 < 0 or c2 >= n or grid[r1][c1] == -1 or grid[r2][c2] == -1: continue // Calculate cherries collected cherries = grid[r1][c1] if r1 != r2 or c1 != c2: cherries += grid[r2][c2] // Calculate maximum cherries from previous positions prev = max( dp[r1-1][c1][r2-1] if r1 > 0 and r2 > 0 else -Infinity, dp[r1-1][c1][r2] if r1 > 0 and r2 < n else -Infinity, dp[r1][c1-1][r2-1] if c1 > 0 and r2 > 0 else -Infinity, dp[r1][c1-1][r2] if c1 > 0 and r2 < n else -Infinity ) // Update DP array if prev != -Infinity: dp[r1][c1][r2] = cherries + prev return max(0, dp[n-1][n-1][n-1])
12345678910111213141516171819202122232425262728293031323334353637383940414243444546function cherryPickup(grid): n = grid.length // Initialize memoization table memo = new 3D array of size n × n × n, initialized with -1 // Define recursive function function dp(r1, c1, r2): // Calculate c2 c2 = r1 + c1 - r2 // Check if position is valid if r1 < 0 or c1 < 0 or r2 < 0 or c2 < 0 or r1 >= n or c1 >= n or r2 >= n or c2 >= n: return -Infinity // Check if position contains a thorn if grid[r1][c1] == -1 or grid[r2][c2] == -1: return -Infinity // Base case: starting position if r1 == 0 and c1 == 0 and r2 == 0: return grid[0][0] // Check if already computed if memo[r1][c1][r2] != -1: return memo[r1][c1][r2] // Calculate cherries collected cherries = grid[r1][c1] if r1 != r2 or c1 != c2: cherries += grid[r2][c2] // Calculate maximum cherries from previous positions prev = max( dp(r1-1, c1, r2-1), dp(r1-1, c1, r2), dp(r1, c1-1, r2-1), dp(r1, c1-1, r2) ) // Update memoization table memo[r1][c1][r2] = cherries + prev return memo[r1][c1][r2] result = dp(n-1, n-1, n-1) return max(0, result)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546function cherryPickup(grid): n = grid.length // Initialize memoization table memo = new 3D array of size n × n × n, initialized with -1 // Define recursive function function dp(r1, c1, r2): // Calculate c2 c2 = r1 + c1 - r2 // Check if position is valid if r1 < 0 or c1 < 0 or r2 < 0 or c2 < 0 or r1 >= n or c1 >= n or r2 >= n or c2 >= n: return -Infinity // Check if position contains a thorn if grid[r1][c1] == -1 or grid[r2][c2] == -1: return -Infinity // Base case: starting position if r1 == 0 and c1 == 0 and r2 == 0: return grid[0][0] // Check if already computed if memo[r1][c1][r2] != -1: return memo[r1][c1][r2] // Calculate cherries collected cherries = grid[r1][c1] if r1 != r2 or c1 != c2: cherries += grid[r2][c2] // Calculate maximum cherries from previous positions prev = max( dp(r1-1, c1, r2-1), dp(r1-1, c1, r2), dp(r1, c1-1, r2-1), dp(r1, c1-1, r2) ) // Update memoization table memo[r1][c1][r2] = cherries + prev return memo[r1][c1][r2] result = dp(n-1, n-1, n-1) return max(0, result)
There are 3 main approaches to solve this problem:
The key insight for solving this problem is to reframe it. Instead of thinking about one person making a round trip, we can think of it as two people starting from (0, 0)
and both reaching (n-1, n-1)
.
At each step, both people can move either right or down, and we need to maximize the total number of cherries they collect. If both people are at the same cell, we should count the cherry only once.
Let's define dp[r1][c1][r2][c2]
as the maximum cherries that can be collected when person 1 is at (r1, c1)
and person 2 is at (r2, c2)
.
The recurrence relation is:
dp[r1][c1][r2][c2] = grid[r1][c1] + grid[r2][c2] - (r1 == r2 && c1 == c2 ? grid[r1][c1] : 0) + max(dp[r1-1][c1][r2-1][c2], dp[r1-1][c1][r2][c2-1], dp[r1][c1-1][r2-1][c2], dp[r1][c1-1][r2][c2-1])
The base case is dp[0][0][0][0] = grid[0][0]
.
However, there's a key observation we can make: if both people take the same number of steps, then r1 + c1 = r2 + c2
. This means we can reduce the 4D DP array to a 3D DP array dp[r1][c1][r2]
, where c2 = r1 + c1 - r2
.
The recurrence relation becomes:
dp[r1][c1][r2] = grid[r1][c1] + (r1 == r2 && c1 == r1 + c1 - r2 ? 0 : grid[r2][r1 + c1 - r2]) + max(dp[r1-1][c1][r2-1], dp[r1-1][c1][r2], dp[r1][c1-1][r2-1], dp[r1][c1-1][r2])
We need to be careful about the boundary conditions and ensure that both people are on valid cells (not thorns).
We need to fill in the DP table of size n^4, and each cell takes constant time to compute.
We use a 4D DP array of size n^4 to store the maximum cherries for each position.
We can optimize the space complexity of the dynamic programming approach by using a 3D array instead of a 4D array.
The key observation is that if both people take the same number of steps, then r1 + c1 = r2 + c2
. This means we can reduce the 4D DP array to a 3D DP array dp[r1][c1][r2]
, where c2 = r1 + c1 - r2
.
Let's define dp[r1][c1][r2]
as the maximum cherries that can be collected when person 1 is at (r1, c1)
and person 2 is at (r2, r1 + c1 - r2)
.
The recurrence relation is:
dp[r1][c1][r2] = grid[r1][c1] + (r1 == r2 && c1 == r1 + c1 - r2 ? 0 : grid[r2][r1 + c1 - r2]) + max(dp[r1-1][c1][r2-1], dp[r1-1][c1][r2], dp[r1][c1-1][r2-1], dp[r1][c1-1][r2])
We need to be careful about the boundary conditions and ensure that both people are on valid cells (not thorns).
We need to fill in the DP table of size n^3, and each cell takes constant time to compute.
We use a 3D DP array of size n^3 to store the maximum cherries for each position.
We can also solve this problem using a top-down approach with memoization, which is essentially a recursive solution with caching to avoid redundant calculations.
Let's define a recursive function dp(r1, c1, r2, c2)
that returns the maximum cherries that can be collected when person 1 is at (r1, c1)
and person 2 is at (r2, c2)
.
The recurrence relation is the same as in the bottom-up approach:
dp(r1, c1, r2, c2) = grid[r1][c1] + grid[r2][c2] - (r1 == r2 && c1 == c2 ? grid[r1][c1] : 0) + max(dp(r1-1, c1, r2-1, c2), dp(r1-1, c1, r2, c2-1), dp(r1, c1-1, r2-1, c2), dp(r1, c1-1, r2, c2-1))
We can optimize this by using the observation that r1 + c1 = r2 + c2
, so we can define dp(r1, c1, r2)
where c2 = r1 + c1 - r2
.
To avoid redundant calculations, we use memoization to store the results of subproblems that we've already solved.
We need to compute the result for each state (r1, c1, r2), and there are n^3 possible states. Each state takes constant time to compute.
We use a memoization table of size n^3 to store the results of subproblems, and the recursion stack can go up to a depth of O(n).
123456789101112131415161718192021222324252627282930313233343536function cherryPickup(grid): n = grid.length // Initialize DP array dp = new 4D array of size (n+1) × (n+1) × (n+1) × (n+1), initialized with -Infinity // Base case dp[0][0][0][0] = grid[0][0] // Fill DP array for r1 from 0 to n-1: for c1 from 0 to n-1: for r2 from 0 to n-1: for c2 from 0 to n-1: // Skip invalid cells if grid[r1][c1] == -1 or grid[r2][c2] == -1: continue // Calculate cherries collected cherries = grid[r1][c1] if r1 != r2 or c1 != c2: cherries += grid[r2][c2] // Calculate maximum cherries from previous positions prev = max( dp[r1-1][c1][r2-1][c2] if r1 > 0 and r2 > 0 else -Infinity, dp[r1-1][c1][r2][c2-1] if r1 > 0 and c2 > 0 else -Infinity, dp[r1][c1-1][r2-1][c2] if c1 > 0 and r2 > 0 else -Infinity, dp[r1][c1-1][r2][c2-1] if c1 > 0 and c2 > 0 else -Infinity ) // Update DP array if prev != -Infinity: dp[r1][c1][r2][c2] = cherries + prev return max(0, dp[n-1][n-1][n-1][n-1])
Understand different approaches to solve the cherry pickup problem and analyze their efficiency.
The key insight for solving this problem is to reframe it. Instead of thinking about one person making a round trip, we can think of it as two people starting from (0, 0)
and both reaching (n-1, n-1)
.
At each step, both people can move either right or down, and we need to maximize the total number of cherries they collect. If both people are at the same cell, we should count the cherry only once.
Let's define dp[r1][c1][r2][c2]
as the maximum cherries that can be collected when person 1 is at (r1, c1)
and person 2 is at (r2, c2)
.
The recurrence relation is:
dp[r1][c1][r2][c2] = grid[r1][c1] + grid[r2][c2] - (r1 == r2 && c1 == c2 ? grid[r1][c1] : 0) + max(dp[r1-1][c1][r2-1][c2], dp[r1-1][c1][r2][c2-1], dp[r1][c1-1][r2-1][c2], dp[r1][c1-1][r2][c2-1])
The base case is dp[0][0][0][0] = grid[0][0]
.
However, there's a key observation we can make: if both people take the same number of steps, then r1 + c1 = r2 + c2
. This means we can reduce the 4D DP array to a 3D DP array dp[r1][c1][r2]
, where c2 = r1 + c1 - r2
.
The recurrence relation becomes:
dp[r1][c1][r2] = grid[r1][c1] + (r1 == r2 && c1 == r1 + c1 - r2 ? 0 : grid[r2][r1 + c1 - r2]) + max(dp[r1-1][c1][r2-1], dp[r1-1][c1][r2], dp[r1][c1-1][r2-1], dp[r1][c1-1][r2])
We need to be careful about the boundary conditions and ensure that both people are on valid cells (not thorns).
We can optimize the space complexity of the dynamic programming approach by using a 3D array instead of a 4D array.
The key observation is that if both people take the same number of steps, then r1 + c1 = r2 + c2
. This means we can reduce the 4D DP array to a 3D DP array dp[r1][c1][r2]
, where c2 = r1 + c1 - r2
.
Let's define dp[r1][c1][r2]
as the maximum cherries that can be collected when person 1 is at (r1, c1)
and person 2 is at (r2, r1 + c1 - r2)
.
The recurrence relation is:
dp[r1][c1][r2] = grid[r1][c1] + (r1 == r2 && c1 == r1 + c1 - r2 ? 0 : grid[r2][r1 + c1 - r2]) + max(dp[r1-1][c1][r2-1], dp[r1-1][c1][r2], dp[r1][c1-1][r2-1], dp[r1][c1-1][r2])
We need to be careful about the boundary conditions and ensure that both people are on valid cells (not thorns).
We can also solve this problem using a top-down approach with memoization, which is essentially a recursive solution with caching to avoid redundant calculations.
Let's define a recursive function dp(r1, c1, r2, c2)
that returns the maximum cherries that can be collected when person 1 is at (r1, c1)
and person 2 is at (r2, c2)
.
The recurrence relation is the same as in the bottom-up approach:
dp(r1, c1, r2, c2) = grid[r1][c1] + grid[r2][c2] - (r1 == r2 && c1 == c2 ? grid[r1][c1] : 0) + max(dp(r1-1, c1, r2-1, c2), dp(r1-1, c1, r2, c2-1), dp(r1, c1-1, r2-1, c2), dp(r1, c1-1, r2, c2-1))
We can optimize this by using the observation that r1 + c1 = r2 + c2
, so we can define dp(r1, c1, r2)
where c2 = r1 + c1 - r2
.
To avoid redundant calculations, we use memoization to store the results of subproblems that we've already solved.
We need to fill in the DP table of size n^4, and each cell takes constant time to compute.
We use a 4D DP array of size n^4 to store the maximum cherries for each position.
We need to fill in the DP table of size n^3, and each cell takes constant time to compute.
We use a 3D DP array of size n^3 to store the maximum cherries for each position.
We need to compute the result for each state (r1, c1, r2), and there are n^3 possible states. Each state takes constant time to compute.
We use a memoization table of size n^3 to store the results of subproblems, and the recursion stack can go up to a depth of O(n).
123456789101112131415161718192021222324252627282930313233343536function cherryPickup(grid): n = grid.length // Initialize DP array dp = new 4D array of size (n+1) × (n+1) × (n+1) × (n+1), initialized with -Infinity // Base case dp[0][0][0][0] = grid[0][0] // Fill DP array for r1 from 0 to n-1: for c1 from 0 to n-1: for r2 from 0 to n-1: for c2 from 0 to n-1: // Skip invalid cells if grid[r1][c1] == -1 or grid[r2][c2] == -1: continue // Calculate cherries collected cherries = grid[r1][c1] if r1 != r2 or c1 != c2: cherries += grid[r2][c2] // Calculate maximum cherries from previous positions prev = max( dp[r1-1][c1][r2-1][c2] if r1 > 0 and r2 > 0 else -Infinity, dp[r1-1][c1][r2][c2-1] if r1 > 0 and c2 > 0 else -Infinity, dp[r1][c1-1][r2-1][c2] if c1 > 0 and r2 > 0 else -Infinity, dp[r1][c1-1][r2][c2-1] if c1 > 0 and c2 > 0 else -Infinity ) // Update DP array if prev != -Infinity: dp[r1][c1][r2][c2] = cherries + prev return max(0, dp[n-1][n-1][n-1][n-1])
1234567891011121314151617181920212223242526272829303132333435363738function cherryPickup(grid): n = grid.length // Initialize DP array dp = new 3D array of size n × n × n, initialized with -Infinity // Base case dp[0][0][0] = grid[0][0] // Fill DP array for r1 from 0 to n-1: for c1 from 0 to n-1: for r2 from 0 to n-1: // Calculate c2 c2 = r1 + c1 - r2 // Skip invalid cells if c2 < 0 or c2 >= n or grid[r1][c1] == -1 or grid[r2][c2] == -1: continue // Calculate cherries collected cherries = grid[r1][c1] if r1 != r2 or c1 != c2: cherries += grid[r2][c2] // Calculate maximum cherries from previous positions prev = max( dp[r1-1][c1][r2-1] if r1 > 0 and r2 > 0 else -Infinity, dp[r1-1][c1][r2] if r1 > 0 and r2 < n else -Infinity, dp[r1][c1-1][r2-1] if c1 > 0 and r2 > 0 else -Infinity, dp[r1][c1-1][r2] if c1 > 0 and r2 < n else -Infinity ) // Update DP array if prev != -Infinity: dp[r1][c1][r2] = cherries + prev return max(0, dp[n-1][n-1][n-1])
12345678910111213141516171819202122232425262728293031323334353637383940414243444546function cherryPickup(grid): n = grid.length // Initialize memoization table memo = new 3D array of size n × n × n, initialized with -1 // Define recursive function function dp(r1, c1, r2): // Calculate c2 c2 = r1 + c1 - r2 // Check if position is valid if r1 < 0 or c1 < 0 or r2 < 0 or c2 < 0 or r1 >= n or c1 >= n or r2 >= n or c2 >= n: return -Infinity // Check if position contains a thorn if grid[r1][c1] == -1 or grid[r2][c2] == -1: return -Infinity // Base case: starting position if r1 == 0 and c1 == 0 and r2 == 0: return grid[0][0] // Check if already computed if memo[r1][c1][r2] != -1: return memo[r1][c1][r2] // Calculate cherries collected cherries = grid[r1][c1] if r1 != r2 or c1 != c2: cherries += grid[r2][c2] // Calculate maximum cherries from previous positions prev = max( dp(r1-1, c1, r2-1), dp(r1-1, c1, r2), dp(r1, c1-1, r2-1), dp(r1, c1-1, r2) ) // Update memoization table memo[r1][c1][r2] = cherries + prev return memo[r1][c1][r2] result = dp(n-1, n-1, n-1) return max(0, result)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546function cherryPickup(grid): n = grid.length // Initialize memoization table memo = new 3D array of size n × n × n, initialized with -1 // Define recursive function function dp(r1, c1, r2): // Calculate c2 c2 = r1 + c1 - r2 // Check if position is valid if r1 < 0 or c1 < 0 or r2 < 0 or c2 < 0 or r1 >= n or c1 >= n or r2 >= n or c2 >= n: return -Infinity // Check if position contains a thorn if grid[r1][c1] == -1 or grid[r2][c2] == -1: return -Infinity // Base case: starting position if r1 == 0 and c1 == 0 and r2 == 0: return grid[0][0] // Check if already computed if memo[r1][c1][r2] != -1: return memo[r1][c1][r2] // Calculate cherries collected cherries = grid[r1][c1] if r1 != r2 or c1 != c2: cherries += grid[r2][c2] // Calculate maximum cherries from previous positions prev = max( dp(r1-1, c1, r2-1), dp(r1-1, c1, r2), dp(r1, c1-1, r2-1), dp(r1, c1-1, r2) ) // Update memoization table memo[r1][c1][r2] = cherries + prev return memo[r1][c1][r2] result = dp(n-1, n-1, n-1) return max(0, result)