Pregunta de entrevista de Meta

Solve a coding question with relatively high efficiency.