Solución
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