Pergunta de entrevista da empresa Block

Leetcode question of Hard difficulty.