Pergunta de entrevista da empresa MathWorks

Very hard dp leetcode type of question