Problem
Ask
Submissions

Problem: Cherry Pickup

Medium
30 min
Understand how to apply dynamic programming techniques to find the maximum cherries collected in a grid. Learn to navigate from start to finish and back while avoiding thorns and optimizing your path.

Statement

You are given an n×nn \times n grid representing a field of cherries. Each cell in the grid can have one of three possible values:

  • 00: An empty cell that can be walked through

  • 11: A cell containing a cherry that can be picked and then passed through

  • 1-1: A cell containing a thorn, which cannot be crossed

Your task is to find the maximum number of cherries that can be collected while following these rules:

  1. Start at the top-left corner (0,0)(0,0) and reach the bottom-right corner (n1, n1)(n-1,~n-1) by only moving right or down through valid cells (00 or 11).

  2. After reaching the bottom-right corner, return to the starting point (0,0)(0, 0) by only moving left or up through valid cells.

  3. When passing through a valid cell containing a cherry, pick it up, and the cell becomes empty (its value changes from 11 to 00).

  4. If there is no valid path between (0,0)(0,0) and (n1, n1)(n-1,~n-1), then no cherries can be collected. Return 00.

Note: A valid path requires traveling from the top-left corner to the bottom-right corner and successfully returning to the starting point.

Constraints:

  • n == grid.length

  • n == grid[i].length

  • 11 \leq n 30\leq 30

  • grid[i][j] is 1-100, or 11.

  • grid[0][0] != 1-1

  • grid[n - 1][n - 1] != 1-1

Problem
Ask
Submissions

Problem: Cherry Pickup

Medium
30 min
Understand how to apply dynamic programming techniques to find the maximum cherries collected in a grid. Learn to navigate from start to finish and back while avoiding thorns and optimizing your path.

Statement

You are given an n×nn \times n grid representing a field of cherries. Each cell in the grid can have one of three possible values:

  • 00: An empty cell that can be walked through

  • 11: A cell containing a cherry that can be picked and then passed through

  • 1-1: A cell containing a thorn, which cannot be crossed

Your task is to find the maximum number of cherries that can be collected while following these rules:

  1. Start at the top-left corner (0,0)(0,0) and reach the bottom-right corner (n1, n1)(n-1,~n-1) by only moving right or down through valid cells (00 or 11).

  2. After reaching the bottom-right corner, return to the starting point (0,0)(0, 0) by only moving left or up through valid cells.

  3. When passing through a valid cell containing a cherry, pick it up, and the cell becomes empty (its value changes from 11 to 00).

  4. If there is no valid path between (0,0)(0,0) and (n1, n1)(n-1,~n-1), then no cherries can be collected. Return 00.

Note: A valid path requires traveling from the top-left corner to the bottom-right corner and successfully returning to the starting point.

Constraints:

  • n == grid.length

  • n == grid[i].length

  • 11 \leq n 30\leq 30

  • grid[i][j] is 1-100, or 11.

  • grid[0][0] != 1-1

  • grid[n - 1][n - 1] != 1-1