MAXCANDY - Editorial

Maximum Candies

Tags: Dynamic Programming, Recursion with memoization.


Author: Shami.

Tester: Kaustav

It may be tempting to solve this problem with greedy strategy but, as the example shows, it gives the wrong answer.

To reach (i, j), there are two options (i-1, j) or (i, j-1) (for legal values of i and j). Let’s just iterate through both of them and take the maximum.

Just don’t forget to cache the values, or it will become an exponential time algorithm!

Top Down (Recursion with memoization)

Bottom up (DP)