Domanda di colloquio di TikTok

Leetcode question. Then try to improve time complexity.