
풀이
재영
const solution = (n) => { const answer = []; const arr = Array.from({ length: n }, () => new Array(n).fill(0)); const directions = [ [1, 0], [0, 1], [-1, -1], ]; let col = 0; let nowValue = 1; let nowDirectionIndex = 0; for (let i = n; i > 0; i -= 1) { const [dRow, dCol] = directions[nowDirectionIndex]; for (let j = 0; j < i; j += 1) { row += dRow; col += dCol; arr[row][col] = nowValue; nowValue += 1; } nowDirectionIndex = (nowDirectionIndex + 1) % 3; } for (let i = 0; i < n; i += 1) { for (let j = 0; j < n; j += 1) { if (arr[i][j]) answer.push(arr[i][j]); } } return answer; };
효성
function solution(n) { const answer = []; let board = Array.from(Array(n), () => Array(n).fill(0)); let count = 0; let curX = -1; let curY = 0; while(n > 0) { for(let i = 0; i < n; i++) { curX += 1; count += 1; board[curX][curY] = count; } for(let i = 0; i < n - 1; i++) { curY += 1; count += 1; board[curX][curY] = count; } for(let i = 0; i < n - 2; i++) { curX -= 1; curY -= 1; count += 1; board[curX][curY] = count; } n -= 3 } const len = board.length; for (let i = 0; i < len; i++) { for (let j = 0; j < len; j++) { if(board[i][j]) { answer.push(board[i][j]); } } } return answer; }