Leetcode Problem 1463. Cherry Pickup II

1463. Cherry Pickup II

Leetcode Solutions

Dynamic Programming (Top Down)

  1. Define a recursive function dp(row, col1, col2) that returns the maximum number of cherries that can be collected from the current state.
  2. If row is the last row, return the cherries at (row, col1) and (row, col2), taking care not to double count if col1 == col2.
  3. If the result for the current state is already computed, return it from the memoization table.
  4. Otherwise, for each possible move combination of the two robots, calculate the cherries collected by calling dp for the next row and updated column positions.
  5. Store the maximum result in the memoization table and return it.
  6. Initialize the memoization table and call dp(0, 0, cols - 1) to start the recursion from the top row with robots at the top-left and top-right corners.
UML Thumbnail

Dynamic Programming (Bottom Up)

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...