Solución

@edied_ramos
·4/4/2026TypeScript
solution.tsTypeScript
function spiralOrder(matrix: number[][]): number[] {
  // Escribe tu solución aquí
  const path = [];
  const n = matrix.length, m = matrix[0].length;
  const directions = [
    [0, 1], [1, 0], [-1, 0], [0, -1]
  ]
  let currentDirection = 0

  const dfs = (i: number, j: number) => {
    if (i >= n || j >= m || i < 0 || j < 0 || matrix[i][j] === Infinity) {
      currentDirection = (currentDirection + 1) % 4
      return;
    }
    path.push(matrix[i][j])
    matrix[i][j] = Infinity
    for (let tryDirection = 0; tryDirection < 4; tryDirection++) {
      dfs(i + directions[currentDirection][0], j + directions[currentDirection][1])
    }
  }
  dfs(0, 0)
  return path;
}

// No modificar: necesario para evaluar el resultado.
export { spiralOrder };
0respuestas
Respuestas
0

Aún no hay respuestas

¡Sé el primero en responder!

Escribir un comentario

Recuerda ser amable. Estás comentando la solución de otra persona. Comparte tu perspectiva de forma constructiva y respetuosa.

Debes iniciar sesión para publicar un comentario.
Markdown