Maximum Cherries Collected by Two Descending Robots

Quick Overview

Maximum Cherries Collected by Two Descending Robots. Two robots start at(0,0) and(0,cols-1) of grid.

Maximum Cherries Collected by Two Descending Robots

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem Two robots start at(0,0) and(0,cols-1) of grid. Each step moves to the next row, changing its column by -1,0,or 1 while remaining inside the grid. Both must reach the last row. A cell's cherries are collected once even if both robots visit it. Return their maximum combined collection. ```cpp class Solution { public: int cherryPickup(vector<vector<int>>& grid); }; ``` ### Examples **Example 1:** ```text Input: grid = [[1,2],[3,4]] Output: 10 Explanation: Keep the robots in their respective columns to collect all four cells:1+2+3+4=10. ``` **Example 2:** ```text Input: grid = [[0,0,0],[0,9,0]] Output: 9 Explanation: At least one robot can reach the center9. Sharing that cell does not collect it twice. ``` ### Constraints - The rectangular grid has 2 to 70 rows and 2 to 70 columns. - Every cherry count lies in `[0,100]`. ### Contract considerations The task returns a deterministic maximum score, not robot paths. ```hint Read the boundary condition Respect both prescribed starting cells and every move boundary. A shared cell contributes its cherries only once. ``` ### Boundary reminders The task returns a deterministic maximum score, not robot paths.

Overview: Maximum Cherries Collected by Two Descending Robots. Two robots start at(0,0) and(0,cols-1) of grid.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Two robots start at(0,0) and(0,cols-1) of grid. Each step moves to the next row, changing its column by -1,0,or 1 while remaining inside the grid. Both must reach the last row. A cell's cherries are collected once even if both robots visit it. Return their maximum combined collection.

class Solution {
public:
    int cherryPickup(vector<vector<int>>& grid);
};

Examples

Example 1:

Input: grid = [[1,2],[3,4]]
Output: 10
Explanation: Keep the robots in their respective columns to collect all four cells:1+2+3+4=10.

Example 2:

Input: grid = [[0,0,0],[0,9,0]]
Output: 9
Explanation: At least one robot can reach the center9. Sharing that cell does not collect it twice.

Constraints

  • The rectangular grid has 2 to 70 rows and 2 to 70 columns.
  • Every cherry count lies in [0,100] .

Contract considerations

The task returns a deterministic maximum score, not robot paths.

Boundary reminders

The task returns a deterministic maximum score, not robot paths.