Pergunta de entrevista da empresa LinkedIn

It is a basic Dynamic Programming question.