Pergunta de entrevista da empresa Microsoft

Question on DP based problem.