ν–‰λ ¬κ³Ό μ—°μ‚°

Deadline
Jan 24, 2023
Status
Archived
Type
Deque
linked list
notion image
notion image
notion image
notion image
notion image
 
πŸ’‘
사싀 이 문제의 μžλ°”μŠ€ν¬λ¦½νŠΈ 정닡을 아직 μ°Ύμ§€ λͺ»ν–ˆμŠ΅λ‹ˆλ‹€(?!) 섀사 ν’€μ§€ λͺ»ν•˜λ”라도, κΌ­ μžμ‹ μ˜ 생각을 λ‹€ν•΄μ„œ 풀어보고 μ˜€μ‹œλŠ” 걸둜 ν•΄μš”! πŸ™†πŸ»

풀이

재영

첫 풀이

μ΅œλŒ€ν•œ ꡬ쑰λ₯Ό μ–΄λ–»κ²Œ μœ μ—°ν•˜κ²Œ 관리할지 μƒκ°ν•˜λ©΄μ„œ μ§œλ³΄μ•˜μŠ΅λ‹ˆλ‹€!
  • 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(); };
notion image
 
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; }