




μ¬μ€ μ΄ λ¬Έμ μ μλ°μ€ν¬λ¦½νΈ μ λ΅μ μμ§ μ°Ύμ§ λͺ»νμ΅λλ€(?!)
μ€μ¬ νμ§ λͺ»νλλΌλ, κΌ μμ μ μκ°μ λ€ν΄μ νμ΄λ³΄κ³ μ€μλ κ±Έλ‘ ν΄μ! ππ»
νμ΄
μ¬μ
첫 νμ΄
μ΅λν ꡬ쑰λ₯Ό μ΄λ»κ² μ μ°νκ² κ΄λ¦¬ν μ§ μκ°νλ©΄μ μ§λ³΄μμ΅λλ€!
MatrixCalculator
: μΌμ’ μ νΌμ¬λμ λλ€. λͺ¨λ μ ν리μΌμ΄μ μ κ΄λ¦¬νλ μΉκ΅¬μ λλ€.
Matrix
: μ΄ λ¬Έμ μ λ°μ΄ν°λ₯Ό λ΄μ, νλ ¬μ νννλ κ°μ²΄μ λλ€.left
μΌμͺ½ λ©΄μ λ΄λΉν©λλ€.right
μ€λ₯Έμͺ½ λ©΄μ λ΄λΉν©λλ€.main
μ€μμ λ΄λΉν©λλ€.
RotateMatrixArrayPrinterStrategy
: μ΄κ²μ΄ κ°μ₯ κ³ λ―Όμ΄ λ§μ΄ λλλ°, κ³Όμ° μ΄print
λΌλ λ©μλκ° λ§μ½Printer
μ΄λΌλ κ°μ²΄μμ μ± μμ κ°μ§κ² λλ€λ©΄, νμ μ΄print
κ° μΌμ ν μκ³ λ¦¬μ¦μΌλ‘ λμ¬μ§μ λν μλ¬Έμ΄ λ€μμ΅λλ€. (μλ₯Ό λ€λ©΄, κ·Έλ₯ ν΄λΌμ΄μΈνΈμ μꡬμ λ°λΌleft
right
main
μ rawνκ² νλ¦°νΈν μλ μκ² μ£ ) λ°λΌμ μ§κΈμ μμμ νμ§ μμμ§λ§ μ λ΅ ν¨ν΄μ μ΄μ©νμ¬ μΆνPrinter
μ΄λΌλ μμ κ°μ²΄κ° μκΈ°λλΌλ μ½κ²print
λΌλ λ©μλμ ννλ₯Ό μ μ§νλλ‘ κ΅¬μ‘°λ₯Ό λμμΈ ν¨ν΄μ μ¬μ©νμ¬ μμ μ± μκ² κ΄λ¦¬νμμ΅λλ€.
κ²°κ³Όλ μ νμ± 100%, ν¨μ¨μ±μμ μ€ν¨(4~8)
class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class Deque { constructor() { this.init(); } init() { this.count = 0; this.front = null; this.rear = null; } unshift(value) { const node = new Node(value); if (!this.front) { this.front = node; this.rear = node; } else { const cachedPrevFront = this.front; cachedPrevFront.prev = node; this.front = node; node.next = cachedPrevFront; } this.count += 1; return this.count; } shift() { if (this.count === 0) return null; const value = this.front.value; if (this.count === 1) { this.init(); } else { this.front = this.front.next; this.front.prev = null; this.count -= 1; } return value; } push(value) { const node = new Node(value); if (this.count === 0) { this.front = node; this.rear = node; } else { const cachedPrevRear = this.rear; cachedPrevRear.next = node; node.prev = cachedPrevRear; this.rear = node; } this.count += 1; return this.count; } pop() { if (this.count === 0) return; const value = this.rear.value; if (this.count === 1) { this.init(); } else { this.rear = this.rear.prev; this.rear.next = null; this.count -= 1; } return value; } getValue(idx) { if (idx >= this.count) return; let node = this.front; for (let i = 0; i < idx; i += 1) { node = node.next; } return node.value; } get length() { return this.count; } } class Queue { constructor(queue) { this.queue = Array.isArray(queue) ? queue : []; this.rear = this.queue.length; this.front = 0; } enqueue(val) { this.queue.push(val); this.rear += 1; } dequeue() { const value = this.queue[this.front]; delete this.queue[this.front]; this.front += 1; return value; } get length() { return this.rear - this.front; } } class MatrixCommandar { constructor({ commands }) { this.taskQueue = new Queue(); this._init(commands); } get TYPE_SHIFT_ROW() { return 'ShiftRow'; } get TYPE_ROTATE() { return 'Rotate'; } _init(commands) { let prev = null; let count = 0; for (let i = 0; i < commands.length; i += 1) { const nowCommands = commands[i]; if (prev === null || prev === nowCommands) { count += 1; } else { this.taskQueue.enqueue([prev, count]); count = 1; } prev = nowCommands; if (i === commands.length - 1) { this.taskQueue.enqueue([prev, count]); } } } command() { if (!this.taskQueue.length) return; // [command, runCount] return this.taskQueue.dequeue(); } get commandLength() { return this.taskQueue.length; } } class Matrix { constructor(matrix) { this.left = new Deque(); this.right = new Deque(); this.main = new Deque(); this._init(matrix); } _init(matrix) { for (let i = 0; i < matrix.length; i += 1) { const row = matrix[i]; const deque = new Deque(); row.forEach((val) => { deque.push(val); }); this.left.push(deque.shift()); this.right.push(deque.pop()); this.main.push(deque); } } rotate(count) { let remainCount = count; while (remainCount) { remainCount -= 1; this.main.front.value.unshift(this.left.shift()); this.right.unshift(this.main.front.value.pop()); this.main.rear.value.push(this.right.pop()); this.left.push(this.main.rear.value.shift()); } } shiftRow(count) { let remainCount = count % this.main.length; while (remainCount) { remainCount -= 1; this.main.unshift(this.main.pop()); this.left.unshift(this.left.pop()); this.right.unshift(this.right.pop()); } } } class RotateMatrixArrayPrinterStrategy { constructor(matrix) { this.matrix = matrix; } print() { let result = []; const matrixLength = this.matrix.main.length; for (let i = 0; i < matrixLength; i += 1) { const row = []; row.push(this.matrix.left.getValue(i)); const shiftedMain = this.matrix.main.getValue(i); for (let j = 0; j < shiftedMain.length; j += 1) { row.push(shiftedMain.getValue(j)); } row.push(this.matrix.right.getValue(i)); result.push(row); } return result; } } class MatrixCalculator { constructor({ commands, matrix, printerStrategy }) { this.commandar = commands; this.matrix = matrix; this.printerStrategy = printerStrategy; } run() { while (this.commandar.commandLength) { const [command, count] = this.commandar.command(); if (command === this.commandar.TYPE_SHIFT_ROW) { this.matrix.shiftRow(count); } if (command === this.commandar.TYPE_ROTATE) { this.matrix.rotate(count); } } } getResult() { return this.printerStrategy.print(); } } const solution = (rc, operations) => { const matrix = new Matrix(rc); const matrixCalculator = new MatrixCalculator({ commands: new MatrixCommandar({ commands: operations }), matrix, printerStrategy: new RotateMatrixArrayPrinterStrategy(matrix), }); matrixCalculator.run(); return matrixCalculator.getResult(); };
2λ²μ§Έ νμ΄
μμ‘΄μ¬ λ°μ λ²λ¦°λ€β¦! κ·Έλ₯ λ§€νΈλ¦μ€ μ΄λ¦¬μ§ μκ³ λ€ λΉΌλ΄λ λ°©μμΌλ‘ ꡬν.
κ²°κ³Ό: 4~6λ²μ ν΅κ³Όνμ§ λͺ»ν¨. π μΈμλΌμ§β¦?
class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class Deque { constructor() { this.init(); } init() { this.count = 0; this.front = null; this.rear = null; } unshift(value) { const node = new Node(value); if (!this.front) { this.front = node; this.rear = node; } else { const cachedPrevFront = this.front; cachedPrevFront.prev = node; this.front = node; node.next = cachedPrevFront; } this.count += 1; return this.count; } shift() { if (this.count === 0) return null; const value = this.front.value; if (this.count === 1) { this.init(); } else { this.front = this.front.next; this.front.prev = null; this.count -= 1; } return value; } push(value) { const node = new Node(value); if (this.count === 0) { this.front = node; this.rear = node; } else { const cachedPrevRear = this.rear; cachedPrevRear.next = node; node.prev = cachedPrevRear; this.rear = node; } this.count += 1; return this.count; } pop() { if (this.count === 0) return; const value = this.rear.value; if (this.count === 1) { this.init(); } else { this.rear = this.rear.prev; this.rear.next = null; this.count -= 1; } return value; } getValue(idx) { if (idx >= this.count) return; let node = this.front; for (let i = 0; i < idx; i += 1) { node = node.next; } return node.value; } get length() { return this.count; } } class Queue { constructor(queue) { this.queue = Array.isArray(queue) ? queue : []; this.rear = this.queue.length; this.front = 0; } enqueue(val) { this.queue.push(val); this.rear += 1; } dequeue() { const value = this.queue[this.front]; delete this.queue[this.front]; this.front += 1; return value; } get length() { return this.rear - this.front; } } class MatrixCommandar { constructor({ commands }) { this.taskQueue = new Queue(); this._init(commands); } get TYPE_SHIFT_ROW() { return 'ShiftRow'; } get TYPE_ROTATE() { return 'Rotate'; } _init(commands) { let prev = null; let count = 0; for (let i = 0; i < commands.length; i += 1) { const nowCommands = commands[i]; if (prev === null || prev === nowCommands) { count += 1; } else { this.taskQueue.enqueue([prev, count]); count = 1; } prev = nowCommands; if (i === commands.length - 1) { this.taskQueue.enqueue([prev, count]); } } } command() { if (!this.taskQueue.length) return; // [command, runCount] return this.taskQueue.dequeue(); } get commandLength() { return this.taskQueue.length; } } class Matrix { constructor(matrix) { this.left = new Deque(); this.right = new Deque(); this.main = new Deque(); this._init(matrix); } _init(matrix) { for (let i = 0; i < matrix.length; i += 1) { const row = matrix[i]; const deque = new Deque(); row.forEach((val) => { deque.push(val); }); this.left.push(deque.shift()); this.right.push(deque.pop()); this.main.push(deque); } } rotate(count) { let remainCount = count; while (remainCount) { remainCount -= 1; this.main.front.value.unshift(this.left.shift()); this.right.unshift(this.main.front.value.pop()); this.main.rear.value.push(this.right.pop()); this.left.push(this.main.rear.value.shift()); } } shiftRow(count) { let remainCount = count % this.main.length; while (remainCount) { remainCount -= 1; this.main.unshift(this.main.pop()); this.left.unshift(this.left.pop()); this.right.unshift(this.right.pop()); } } } class RotateMatrixArrayPrinterStrategy { constructor(matrix) { this.matrix = matrix; } print() { let result = []; const matrixLength = this.matrix.main.length; for (let i = 0; i < matrixLength; i += 1) { const row = []; row.push(this.matrix.left.shift()); const shiftedMain = this.matrix.main.getValue(i); const shiftedMainLength = shiftedMain.length; for (let j = 0; j < shiftedMainLength; j += 1) { row.push(shiftedMain.shift()); } row.push(this.matrix.right.shift()); result.push(row); } return result; } } class MatrixCalculator { constructor({ commands, matrix, printerStrategy }) { this.commandar = commands; this.matrix = matrix; this.printerStrategy = printerStrategy; } run() { while (this.commandar.commandLength) { const [command, count] = this.commandar.command(); if (command === this.commandar.TYPE_SHIFT_ROW) { this.matrix.shiftRow(count); } if (command === this.commandar.TYPE_ROTATE) { this.matrix.rotate(count); } } } getResult() { return this.printerStrategy.print(); } } const solution = (rc, operations) => { const matrix = new Matrix(rc); const matrixCalculator = new MatrixCalculator({ commands: new MatrixCommandar({ commands: operations }), matrix, printerStrategy: new RotateMatrixArrayPrinterStrategy(matrix), }); matrixCalculator.run(); return matrixCalculator.getResult(); }; { const rc = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ]; const operations = ['Rotate', 'ShiftRow']; console.log(solution(rc, operations)); } { // [[8, 3, 3], [4, 9, 7], [3, 8, 6]] const rc = [ [8, 6, 3], [3, 3, 7], [8, 4, 9], ]; const operations = ['Rotate', 'ShiftRow', 'ShiftRow']; console.log(solution(rc, operations)); } { // [[1, 6, 7 ,8], [5, 9, 10, 4], [2, 3, 12, 11]] const rc = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ]; const operations = ['ShiftRow', 'Rotate', 'ShiftRow', 'Rotate']; console.log(solution(rc, operations)); } { /** * 2 1 * 3 6 * 5 4 */ const rc = [ [1, 2], [3, 4], [5, 6], ]; const operations = ['Rotate', 'ShiftRow', 'Rotate', 'ShiftRow']; console.log(solution(rc, operations)); }
3λ²μ§Έ νμ΄(ν΅κ³Ό)
μβ¦! λ§μ§λ§μ
print
ν λ shiftedMain
μ μΈλ±μ€λ‘ μ κ·Όν΄μ κ°κ³ μλ€.μ΄ μμ
shift()
νλ©΄ O(Index)
λ₯Ό O(1)
λ‘ μ€μΌ μ μκ² λ€!class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class Deque { constructor() { this.init(); } init() { this.count = 0; this.front = null; this.rear = null; } unshift(value) { const node = new Node(value); if (!this.front) { this.front = node; this.rear = node; } else { const cachedPrevFront = this.front; cachedPrevFront.prev = node; this.front = node; node.next = cachedPrevFront; } this.count += 1; return this.count; } shift() { if (this.count === 0) return null; const value = this.front.value; if (this.count === 1) { this.init(); } else { this.front = this.front.next; this.front.prev = null; this.count -= 1; } return value; } push(value) { const node = new Node(value); if (this.count === 0) { this.front = node; this.rear = node; } else { const cachedPrevRear = this.rear; cachedPrevRear.next = node; node.prev = cachedPrevRear; this.rear = node; } this.count += 1; return this.count; } pop() { if (this.count === 0) return; const value = this.rear.value; if (this.count === 1) { this.init(); } else { this.rear = this.rear.prev; this.rear.next = null; this.count -= 1; } return value; } getValue(idx) { if (idx >= this.count) return; let node = this.front; for (let i = 0; i < idx; i += 1) { node = node.next; } return node.value; } get length() { return this.count; } } class Queue { constructor(queue) { this.queue = Array.isArray(queue) ? queue : []; this.rear = this.queue.length; this.front = 0; } enqueue(val) { this.queue.push(val); this.rear += 1; } dequeue() { const value = this.queue[this.front]; delete this.queue[this.front]; this.front += 1; return value; } get length() { return this.rear - this.front; } } class MatrixCommandar { constructor({ commands }) { this.taskQueue = new Queue(); this._init(commands); } get TYPE_SHIFT_ROW() { return 'ShiftRow'; } get TYPE_ROTATE() { return 'Rotate'; } _init(commands) { let prev = null; let count = 0; for (let i = 0; i < commands.length; i += 1) { const nowCommands = commands[i]; if (prev === null || prev === nowCommands) { count += 1; } else { this.taskQueue.enqueue([prev, count]); count = 1; } prev = nowCommands; if (i === commands.length - 1) { this.taskQueue.enqueue([prev, count]); } } } command() { if (!this.taskQueue.length) return; // [command, runCount] return this.taskQueue.dequeue(); } get commandLength() { return this.taskQueue.length; } } class Matrix { constructor(matrix) { this.left = new Deque(); this.right = new Deque(); this.main = new Deque(); this._init(matrix); } _init(matrix) { for (let i = 0; i < matrix.length; i += 1) { const row = matrix[i]; const deque = new Deque(); row.forEach((val) => { deque.push(val); }); this.left.push(deque.shift()); this.right.push(deque.pop()); this.main.push(deque); } } rotate(count) { let remainCount = count; while (remainCount) { remainCount -= 1; this.main.front.value.unshift(this.left.shift()); this.right.unshift(this.main.front.value.pop()); this.main.rear.value.push(this.right.pop()); this.left.push(this.main.rear.value.shift()); } } shiftRow(count) { let remainCount = count % this.main.length; while (remainCount) { remainCount -= 1; this.main.unshift(this.main.pop()); this.left.unshift(this.left.pop()); this.right.unshift(this.right.pop()); } } } class RotateMatrixArrayPrinterStrategy { constructor(matrix) { this.matrix = matrix; } print() { let result = []; const matrixLength = this.matrix.main.length; for (let i = 0; i < matrixLength; i += 1) { const row = []; row.push(this.matrix.left.shift()); const shiftedMain = this.matrix.main.shift(); const shiftedMainLength = shiftedMain.length; for (let j = 0; j < shiftedMainLength; j += 1) { row.push(shiftedMain.shift()); } row.push(this.matrix.right.shift()); result.push(row); } return result; } } class MatrixCalculator { constructor({ commands, matrix, printerStrategy }) { this.commandar = commands; this.matrix = matrix; this.printerStrategy = printerStrategy; } run() { while (this.commandar.commandLength) { const [command, count] = this.commandar.command(); if (command === this.commandar.TYPE_SHIFT_ROW) { this.matrix.shiftRow(count); } if (command === this.commandar.TYPE_ROTATE) { this.matrix.rotate(count); } } } getResult() { return this.printerStrategy.print(); } } const solution = (rc, operations) => { const matrix = new Matrix(rc); const matrixCalculator = new MatrixCalculator({ commands: new MatrixCommandar({ commands: operations }), matrix, printerStrategy: new RotateMatrixArrayPrinterStrategy(matrix), }); matrixCalculator.run(); return matrixCalculator.getResult(); };

14λͺ
μ μ λ΅μ μ€ κ°μ₯ κΈ΄ μ½λμ 2λ°°μ μ‘λ°νλ μ½λλ₯Ό λ¨κΈ°κ³ λ λλ€β¦(?!)
κ·Έλ°λ°, λ€λ₯Έ λΆλ€μ νμ΄λ€ 보μνλ μ λ§ μ’μ νμ΄λ€ λ§μμ νκΈΈ μνλ€ π₯°
λ¨μ°μ»¨λ, λ΄κ° κ·Όλ λ³Έ λ¬Έμ μ€ κ°μ₯ λ©μ§ λ¬Έμ λΌ μκ°νλ€.
κ°μ₯ κΈ°λ³Έμ μΈ μλ£κ΅¬μ‘°λ€λ§μΌλ‘, μκ°νλ νμ μΆ©λΆν κΈ°λ₯Ό μ μλ λ¬Έμ μλ€.
μ¬λ¬λΆλ μκ°μ΄ λλ©΄ κΌ νμ΄λ³΄μκΈΈ!
μ΅μ ν
μκ°ν΄λ³΄λ
rotate
λ ν
λ리λ§νΌ νμ νλ©΄ μ μμΉλ‘ λμμ€λ―λ‘, μ΄μ λ§μΆ° λλ¨Έμ§λ§ κ³μ°νλλ‘ μ μ©ν©λλ€!class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } class Deque { constructor() { this.init(); } init() { this.count = 0; this.front = null; this.rear = null; } unshift(value) { const node = new Node(value); if (!this.front) { this.front = node; this.rear = node; } else { const cachedPrevFront = this.front; cachedPrevFront.prev = node; this.front = node; node.next = cachedPrevFront; } this.count += 1; return this.count; } shift() { if (this.count === 0) return null; const value = this.front.value; if (this.count === 1) { this.init(); } else { this.front = this.front.next; this.front.prev = null; this.count -= 1; } return value; } push(value) { const node = new Node(value); if (this.count === 0) { this.front = node; this.rear = node; } else { const cachedPrevRear = this.rear; cachedPrevRear.next = node; node.prev = cachedPrevRear; this.rear = node; } this.count += 1; return this.count; } pop() { if (this.count === 0) return; const value = this.rear.value; if (this.count === 1) { this.init(); } else { this.rear = this.rear.prev; this.rear.next = null; this.count -= 1; } return value; } getValue(idx) { if (idx >= this.count) return; let node = this.front; for (let i = 0; i < idx; i += 1) { node = node.next; } return node.value; } get rawArray() { let arr = []; let node = this.front; for (let i = 0; i < this.count; i += 1) { arr.push(node.value); node = node.next; } return arr; } get length() { return this.count; } } class Queue { constructor(queue) { this.queue = Array.isArray(queue) ? queue : []; this.rear = this.queue.length; this.front = 0; } enqueue(val) { this.queue.push(val); this.rear += 1; } dequeue() { const value = this.queue[this.front]; delete this.queue[this.front]; this.front += 1; return value; } get length() { return this.rear - this.front; } } class MatrixCommandar { constructor({ commands }) { this.taskQueue = new Queue(); this._init(commands); } get TYPE_SHIFT_ROW() { return 'ShiftRow'; } get TYPE_ROTATE() { return 'Rotate'; } _init(commands) { let prev = null; let count = 0; for (let i = 0; i < commands.length; i += 1) { const nowCommands = commands[i]; if (prev === null || prev === nowCommands) { count += 1; } else { this.taskQueue.enqueue([prev, count]); count = 1; } prev = nowCommands; if (i === commands.length - 1) { this.taskQueue.enqueue([prev, count]); } } } command() { if (!this.taskQueue.length) return; // [command, runCount] return this.taskQueue.dequeue(); } get commandLength() { return this.taskQueue.length; } } class Matrix { constructor(matrix) { this.left = new Deque(); this.right = new Deque(); this.main = new Deque(); this._init(matrix); } _init(matrix) { for (let i = 0; i < matrix.length; i += 1) { const row = matrix[i]; const deque = new Deque(); row.forEach((val) => { deque.push(val); }); this.left.push(deque.shift()); this.right.push(deque.pop()); this.main.push(deque); } } rotate(count) { let remainCount = count % (this.main.front.value.length * 2 + this.left.length + this.right.length); while (remainCount) { remainCount -= 1; this.main.front.value.unshift(this.left.shift()); this.right.unshift(this.main.front.value.pop()); this.main.rear.value.push(this.right.pop()); this.left.push(this.main.rear.value.shift()); } } shiftRow(count) { let remainCount = count % this.main.length; while (remainCount) { remainCount -= 1; this.main.unshift(this.main.pop()); this.left.unshift(this.left.pop()); this.right.unshift(this.right.pop()); } } } class RotateMatrixArrayPrinterStrategy { constructor(matrix) { this.matrix = matrix; } print() { let result = []; const leftArr = this.matrix.left.rawArray; const mainArr = this.matrix.main.rawArray; const rightArr = this.matrix.right.rawArray; for (let i = 0; i < mainArr.length; i += 1) { const row = []; row.push(leftArr[i]); row.push(...mainArr[i].rawArray); row.push(rightArr[i]); result.push(row); } return result; } } class MatrixCalculator { constructor({ commands, matrix, printerStrategy }) { this.commandar = commands; this.matrix = matrix; this.printerStrategy = printerStrategy; } run() { while (this.commandar.commandLength) { const [command, count] = this.commandar.command(); if (command === this.commandar.TYPE_SHIFT_ROW) { this.matrix.shiftRow(count); } if (command === this.commandar.TYPE_ROTATE) { this.matrix.rotate(count); } } } getResult() { return this.printerStrategy.print(); } } const solution = (rc, operations) => { const matrix = new Matrix(rc); const matrixCalculator = new MatrixCalculator({ commands: new MatrixCommandar({ commands: operations }), matrix, printerStrategy: new RotateMatrixArrayPrinterStrategy(matrix), }); matrixCalculator.run(); return matrixCalculator.getResult(); };
ν¨μ±
μκ°μ΄κ³Ό
function solution(rc, operations) { let answer = rc; const rLen = rc.length; const cLen = rc[0].length; for(let idx = 0; idx < operations.length; idx++) { const operation = operations[idx]; if(operation === 'ShiftRow') { const last = answer.pop(); answer.splice(0, 0, last); } else if(operation === 'Rotate') { const flattenArr = []; flattenArr.push(...answer[0]); for(let i = 1; i < rLen - 1; i++) { flattenArr.push(answer[i][cLen - 1]); } flattenArr.push(...answer[rLen - 1].reverse()); for(let i = rLen - 2; i > 0; i--) { flattenArr.push(answer[i][0]); } const last = flattenArr.pop(); flattenArr.splice(0, 0, last); let pointerIdx = 0; for(let i = 0; i < cLen; i++) { answer[0][i] = flattenArr[pointerIdx]; pointerIdx += 1; } for(let i = 1; i < rLen - 1; i++) { answer[i][cLen - 1] = flattenArr[pointerIdx]; pointerIdx += 1; } for(let i = cLen - 1; i >= 0; i--) { answer[rLen - 1][i] = flattenArr[pointerIdx]; pointerIdx += 1; } for(let i = rLen - 2; i > 0; i--) { answer[i][0] = flattenArr[pointerIdx]; pointerIdx += 1; } } } return answer; }