Tutorialspoint
Problem
Solution
Submissions

Cherry Pickup

Certification: Advanced Level Accuracy: 100% Submissions: 1 Points: 15

Write a Java program to solve the Cherry Pickup problem. You are given an n x n grid representing a field of cherries, each cell is either empty (0) or contains one cherry (1). You need to collect maximum number of cherries possible by following these rules: - Start at position (0, 0) and collect cherries while moving to position (n-1, n-1) - Then return back from (n-1, n-1) to (0, 0), collecting cherries again - When you pass through a cell, you pick up all cherries, and the cell becomes empty - You can only move right or down during the first trip, and left or up during the return trip - The grid is given as a 2D array where grid[i][j] represents the number of cherries at position (i, j)

Example 1
  • Input: grid = [
    [0, 1, -1],
    [1, 0, -1],
    [1, 1, 1]
    ]
  • Output: 5
  • Explanation:
    Step 1: The grid is 3x3 with cherries (1), empty cells (0), and thorns (-1).
    Step 2: First trip path: (0,0) → (0,1) → (1,1) → (2,1) → (2,2)
    Step 3: Cherries collected: 0 + 1 + 0 + 1 + 1 = 3
    Step 4: Return trip path: (2,2) → (2,1) → (1,1) → (0,1) → (0,0)
    Step 5: Additional cherries: 0 + 0 + 0 + 0 + 0 = 0 (all already collected)
    Step 6: Max total cherries: 3 + 0 = 3
    Step 7: But there's a better path: (0,0) → (1,0) → (2,0) → (2,1) → (2,2) and back, giving 5 cherries.
Example 2
  • Input: grid = [
    [1, 1, 1, 1, 0, 0, 0],
    [0, 0, 0, 1, 0, 0, 0],
    [0, 0, 0, 1, 0, 0, 1],
    [1, 0, 0, 1, 0, 0, 0],
    [0, 0, 0, 1, 0, 0, 0],
    [0, 0, 0, 1, 0, 0, 0],
    [0, 0, 0, 1, 1, 1, 1]
    ]
  • Output: 15
  • Explanation:
    Step 1: The grid is 7x7 with cherries (1) and empty cells (0).
    Step 2: The optimal path goes along the right border and then bottom border for the first trip.
    Step 3: Then returns following the same path, but no additional cherries are collected.
    Step 4: Total cherries collected: 15.
Constraints
  • 1 <= n <= 50
  • grid[i][j] is 0, 1, or -1
  • grid[0][0] != -1
  • grid[n-1][n-1] != -1
  • Time Complexity: O(n³)
  • Space Complexity: O(n³)
ArraysCapgeminiAirbnb
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Suppose we are having two people starting from (0,0) and both reaching (n-1, n-1)
  • Both people move simultaneously, collecting cherries
  • If both land on the same cell, count the cherry only once
  • Use dynamic programming with state (row1, col1, row2) where col2 can be derived
  • The base case is when both reach (n-1, n-1)
  • For each state, try all four combinations of moves (right/down for each person)
  • Use memoization to avoid redundant calculations

Steps to solve by this approach:

 Step 1: Realize that the problem can be reframed as having two people simultaneously traversing from (0,0) to (n-1,n-1).

 Step 2: Define a state as (r1, c1, r2), where (r1, c1) is the position of the first person and (r2, c2) is the position of the  second person.
 Step 3: Calculate c2 using the fact that both people have traveled the same distance: r1 + c1 = r2 + c2.
 Step 4: Use dynamic programming with memoization to avoid redundant calculations for the same state.
 Step 5: For each state, try all four possible combinations of moves (right/down for each person).
 Step 6: If both people land on the same cell, count the cherry only once.
 Step 7: Return the maximum cherries that can be collected.

Submitted Code :