Uber Interview Question

recursion question with DP follow up