Pregunta de entrevista de Yandex

Some leetcode task, which wasn't hard