Yandex interview question

Some leetcode task, which wasn't hard