Amazon interview question

leetcode hard question using dynamic programming