Pregunta de entrevista de Amazon

one hard question from Dynamic programming