Uber interview question

Hard Leetcode question related to dynamic programming.