Pergunta de entrevista da empresa Google

As much as I recall it is a Dynamic Programming question.