Amazon Interview Question

dynamic programming question from leetcode