Pregunta de entrevista de TikTok

Leetcode question. Then try to improve time complexity.