1141. Cherry Pickup I — Max Cherries

EasyDynamic ProgrammingGrid DP

Given a grid where each cell holds 1 (a cherry), 0 (empty), or -1 (a thorn that blocks movement), start at the top-left and move only right or down to reach the bottom-right, collecting cherries. Return the maximum cherries collected on one trip, or 0 if the bottom-right is unreachable. The input is JSON {grid}.

Input: JSON {grid}.

Output: Integer — the maximum cherries collected.

Examples

Example 1
Input: {"grid":[[0,1,-1],[1,0,-1],[1,1,1]]}
Output: 4
Explanation: Collect four cherries down the left side.
Example 2
Input: {"grid":[[1,1],[1,1]]}
Output: 3
Explanation: Collect three on one trip.
Example 3
Input: {"grid":[[1]]}
Output: 1
Explanation: Single cherry.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →