741. Cherry Pickup LeetCode Solution
In this guide, you will get 741. Cherry Pickup LeetCode Solution with the best time and space complexity. The solution to Cherry Pickup problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Cherry Pickup solution in C++
- Cherry Pickup solution in Java
- Cherry Pickup solution in Python
- Additional Resources

Problem Statement of Cherry Pickup
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:
Starting at the position (0, 0) and reaching (n – 1, n – 1) by moving right or down through valid path cells (cells with value 0 or 1).
After reaching (n – 1, n – 1), returning to (0, 0) by moving left or up through valid path cells.
When passing through a path cell containing a cherry, you pick it up, and the cell becomes an empty cell 0.
If there is no valid path between (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.length
n == grid[i].length
1 <= n <= 50
grid[i][j] is -1, 0, or 1.
grid[0][0] != -1
grid[n – 1][n – 1] != -1
Complexity Analysis
- Time Complexity: O(n^3)
- Space Complexity: O(n^3)
741. Cherry Pickup LeetCode Solution in C++
class Solution {
public:
int cherryPickup(vector<vector<int>>& grid) {
// The problem is identical as two people start picking cherries from
// grid[0][0] simultaneously.
const int n = grid.size();
vector<vector<vector<int>>> mem(
n + 1, vector<vector<int>>(n + 1, vector<int>(n + 1, INT_MIN)));
return max(0, cherryPickup(grid, 0, 0, 0, mem));
}
private:
// Returns the maximum cherries we could pick from g[0][0] ->
// g[x1 - 1][y1 - 1] + g[0][0] -> g[x2 - 1][y2 - 1], where y2 = x1 + y1 - x2
// (reducing states from 4 to 3).
int cherryPickup(const vector<vector<int>>& grid, int x1, int y1, int x2,
vector<vector<vector<int>>>& mem) {
const int n = grid.size();
const int y2 = x1 + y1 - x2;
if (x1 == n || y1 == n || x2 == n || y2 == n)
return -1;
if (x1 == n - 1 && y1 == n - 1)
return grid[x1][y1];
if (grid[x1][y1] == -1 || grid[x2][y2] == -1)
return -1;
int& res = mem[x1][y1][x2];
if (mem[x1][y1][x2] > INT_MIN)
return res;
res = max({cherryPickup(grid, x1 + 1, y1, x2, mem),
cherryPickup(grid, x1 + 1, y1, x2 + 1, mem),
cherryPickup(grid, x1, y1 + 1, x2, mem),
cherryPickup(grid, x1, y1 + 1, x2 + 1, mem)});
if (res == -1)
return res;
res += grid[x1][y1]; // Pick some cherries.
if (x1 != x2) // Two people are on the different grids.
res += grid[x2][y2];
return res;
}
};
/* code provided by PROGIEZ */
741. Cherry Pickup LeetCode Solution in Java
class Solution {
public int cherryPickup(int[][] grid) {
// The problem is identical as two people start picking cherries from
// grid[0][0] simultaneously.
final int n = grid.length;
Integer[][][] mem = new Integer[n][n][n];
return Math.max(0, cherryPickup(grid, 0, 0, 0, mem));
}
// Returns the maximum cherries we could pick from g[0][0] ->
// g[x1 - 1][y1 - 1] + g[0][0] -> g[x2 - 1][y2 - 1], where y2 = x1 + y1 - x2
// (reducing states from 4 to 3).
private int cherryPickup(int[][] grid, int x1, int y1, int x2, Integer[][][] mem) {
final int n = grid.length;
final int y2 = x1 + y1 - x2;
if (x1 == n || y1 == n || x2 == n || y2 == n)
return -1;
if (x1 == n - 1 && y1 == n - 1)
return grid[x1][y1];
if (grid[x1][y1] == -1 || grid[x2][y2] == -1)
return -1;
Integer res = mem[x1][y1][x2];
if (res != null)
return res;
res = Math.max(Math.max(cherryPickup(grid, x1 + 1, y1, x2, mem), //
cherryPickup(grid, x1 + 1, y1, x2 + 1, mem)),
Math.max(cherryPickup(grid, x1, y1 + 1, x2, mem), //
cherryPickup(grid, x1, y1 + 1, x2 + 1, mem)));
if (res == -1)
return mem[x1][y1][x2] = res;
res += grid[x1][y1]; // Pick some cherries.
if (x1 != x2) // Two people are on the different grids.
res += grid[x2][y2];
return mem[x1][y1][x2] = res;
}
}
// code provided by PROGIEZ
741. Cherry Pickup LeetCode Solution in Python
class Solution {
public:
int cherryPickup(vector<vector<int>>& grid) {
const int n = grid.size();
// dp[x1][y1][x2] := the maximum cherries we could pick from
// g[0][0] -> g[x1 - 1][y1 - 1] + g[0][0] -> g[x2 - 1][y2 - 1],
// where y2 = x1 + y1 - x2 (reducing states from 4 to 3)
vector<vector<vector<int>>> dp(
n + 1, vector<vector<int>>(n + 1, vector<int>(n + 1, -1)));
dp[1][1][1] = grid[0][0];
for (int x1 = 1; x1 <= n; ++x1)
for (int y1 = 1; y1 <= n; ++y1)
for (int x2 = 1; x2 <= n; ++x2) {
const int y2 = x1 + y1 - x2;
if (y2 < 1 || y2 > n)
continue;
if (grid[x1 - 1][y1 - 1] == -1 || grid[x2 - 1][y2 - 1] == -1)
continue;
const int ans = max({dp[x1 - 1][y1][x2], dp[x1 - 1][y1][x2 - 1],
dp[x1][y1 - 1][x2], dp[x1][y1 - 1][x2 - 1]});
if (ans < 0)
continue;
dp[x1][y1][x2] = ans + grid[x1 - 1][y1 - 1];
if (x1 != x2)
dp[x1][y1][x2] += grid[x2 - 1][y2 - 1];
}
return max(0, dp[n][n][n]);
}
};
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.