Pregunta de entrevista de Microsoft

Question on DP based problem.