Pregunta de entrevista de Google

Dynamic programming question. But mostly everything was normal