Leetcode Problem 741. Cherry Pickup

741. Cherry Pickup

Leetcode Solutions

Dynamic Programming (Bottom Up)

  1. Initialize a 2D array dp with dimensions n x n to store the maximum number of cherries for each position pair (c1, c2) at each layer t.
  2. Iterate over each layer t from 0 to 2 * (n - 1).
  3. Create a temporary 2D array dp2 to store the results for the current layer.
  4. For each possible position (c1, c2) at layer t, calculate r1 = t - c1 and r2 = t - c2.
  5. If (r1, c1) and (r2, c2) are within bounds and not thorns, calculate the maximum cherries by considering all possible moves for both people.
  6. Update dp2[c1][c2] with the calculated maximum cherries.
  7. After processing the current layer, assign dp2 to dp.
  8. The answer is dp[0][0] after processing all layers.
UML Thumbnail

Dynamic Programming (Top Down)

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...