MathWorks Interview Question

Very hard dp leetcode type of question