Pregunta de entrevista de Meta

Asked leetcode hard Question.