Amazon interview question

dynamic programming question from leetcode