Pregunta de entrevista de TuSimple

a medium difficulty leetcode question.