Cherry pickup II

Dynamic Programming DP on grids Medium
  • This problem and its underlying algorithm concepts can find use in real-world applications like video games and robotics
  • In a gaming scenario, this problem can be used to program artificial intelligence for characters in grid-based turn strategy games, ensuring each character gathers the most resources or points while avoiding collision
  • In robotics, this might be a real scenario where two robots are tasked to collect items from a given space and the optimized path needs to be calculated to collect maximum items without both robots reaching the same point at the same time
  • This coordination and optimization problem requires advanced algorithms for efficiency and is a fundamental challenge in multi-robot systems

Given a n x m 2d integer array called matrix where matrix[i][j] represents the number of cherries you can pick up from the (i, j) cell.Given two robots that can collect cherries, one is located at the top-leftmost (0, 0) cell and the other at the top-rightmost (0, m-1) cell.


Return the maximum number of cherries that can be picked by the two robots in total, following these rules:

  • Robots that are standing on (i, j) cell can only move to cell (i + 1, j - 1), (i + 1, j), or (i + 1, j + 1), if it exists in the matrix.
  • A robot will pick up all the cherries in a given cell when it passes through that cell.
  • If both robots come to the same cell at the same time, only one robot takes the cherries.
  • Both robots must reach the bottom row in matrix.

Examples:

Input: matrix = [[2, 1, 3], [4, 2, 5], [1, 6, 2], [7, 2, 8]]

Output: 37

Explanation: Possible left robot path:-

Start at 0th cell (2) -> down (4) -> down-right (6) ->down-left (7)

Possible right robot path:-

Start at 2nd cell (3) -> down (5) -> down (2) -> down (8)

Input: matrix = [[1, 4, 4, 1], [1, 2, 2, 1], [5, 6, 10, 11], [8, 1, 1, 1]]

Output: 32

Explanation: Possible left robot path:-

Start at 0th cell (1) -> down-right (2) -> down (6) ->down-left (8)

Possible right robot path:-

Start at 3rd cell (1) -> down-left (2) -> down-right (11) -> down (1)

Input: matrix = [[1, 2, 3], [5, 4, 6], [4, 4, 1]]

Constraints

  • n == number of rows in matrix
  • m == number of columns in matrix
  • 2 <= n, m <= 70
  • 0 <= matrix[i][j] <= 1000

Hints

  • A recursive approach with memoization is a natural way to solve this problem. Define a function that represents the maximum cherries collected when both robots are at specific positions in a given row. The function should consider all valid moves for both robots and return the optimal value.
  • To optimize further, an iterative bottom-up DP approach can be used. Instead of computing values recursively, maintain a DP table where each entry stores the best possible number of cherries collected when the robots are in specific positions. Start from the last row and work upward, simulating the robots' movement constraints.

Company Tags

Chewy Roblox Ubisoft IBM Seagate Technology Medtronic Philips Healthcare Rakuten Robinhood Qualcomm DoorDash Airbnb Square Alibaba PwC Etsy Red Hat Shopify GE Healthcare Texas Instruments Broadcom OYO Rooms eBay Ernst & Young Zomato Google Microsoft Amazon Meta Apple Netflix Adobe