Cherry Pickup II

HardDynamic Programming2d dp

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given a rows x cols grid where grid[r][c] is the number of cherries in that cell. Two robots start on the top row: robot A in the top-left cell (0, 0) and robot B in the top-right cell (0, cols - 1). On each step both robots move down exactly one row, and each may shift its column by -1, 0, or +1, staying inside the grid. They stop once they reach the bottom row. A robot collects all the cherries in every cell it visits (including its starting cell); if both robots stand on the same cell, those cherries are collected only once. Return the maximum total number of cherries the two robots can collect together.

Examples

Input: grid = [[3,1,1],[2,5,1],[1,5,5],[2,1,1]]
Output: 24
Robot A takes 3, 2, 5, 2 (12) and robot B takes 1, 5, 5, 1 (12), for 24 total.
Input: grid = [[1,0,0,0,0,0,1],[2,0,0,0,0,3,0],[2,0,9,0,0,0,0],[0,3,0,5,4,0,0],[1,0,2,3,0,0,6]]
Output: 28
Robot A collects 1 + 9 + 5 + 2 = 17 and robot B collects 1 + 3 + 4 + 3 = 11.

Constraints

  • 2 <= rows, cols <= 70
  • 0 <= grid[r][c] <= 100

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More 2d dp problems