문제










풀이
재영
넘나 어렵다~~ 안보고 풀었다는 것에 큰 박수 👏🏻👏🏻👏🏻👏🏻
class LinkedList { constructor(value, n) { this.result = []; this.nowNode = null; this.idx = null; this.head = null; this.tail = null; this.lastIndex = null; this.initialize(value, n); } initialize(value, n) { this.result = new Array(n).fill("O"); this.nowNode = this.getNewNode(value); this.nowNode.prev = this.nowNode; this.nowNode.next = this.nowNode; this.idx = 0; this.head = this.nowNode; this.tail = this.nowNode; this.lastIndex = n - 1; } append(value) { const newNode = this.getNewNode(value, this.tail, this.head); this.tail.next = newNode; this.tail = newNode; this.head.prev = newNode; this.lastIndex = value; return this; } remove() { const targetNode = this.nowNode; const { prev, next, value } = targetNode; prev.next = next; next.prev = prev; this.result[value] = "X"; if (this.lastIndex === value) { this.lastIndex = prev.value; this.nowNode = prev; } else { this.nowNode = next; } return targetNode; } insert(node) { const { prev, next, value } = node; prev.next = node; next.prev = node; this.result[node.value] = "O"; if (this.lastIndex < value) { this.lastIndex = value; } } getNewNode(value, prev = null, next = null) { return { value, prev, next, }; } moveUp(cnt) { for (let i = 0; i < cnt; i += 1) { this.nowNode = this.nowNode.prev; } } moveDown(cnt) { for (let i = 0; i < cnt; i += 1) { this.nowNode = this.nowNode.next; } } } const solution = (n, k, cmd) => { const removeStore = []; const commandHandlers = { C: () => removeStore.push(list.remove()), U: (cnt) => list.moveUp(cnt), D: (cnt) => list.moveDown(cnt), Z: () => list.insert(removeStore.pop()), }; const list = new LinkedList(0, n); for (let i = 1; i < n; i += 1) { list.append(i); } list.moveDown(k); cmd.forEach((command) => { const [nowCommand, cnt] = command.split(" "); commandHandlers[nowCommand](cnt); }); return list.result.join(""); };
효성
실패 풀이
length가 달라지는 상황에서 compare의 길이는 일정한 채로 curIdx를 판단 지표로 사용하는 게 말이 안됨!
function solution(n, k, cmd) { let compare = Array(n).fill('O'); let curIdx = k; let length = n; let savedIdxArr = []; cmd.forEach(item => { const [command, cnt] = item.split(" "); if(command === 'U') { curIdx -= Number(cnt); } if(command === 'D') { curIdx += Number(cnt); } if(command === 'C') { compare[curIdx] = 'X'; savedIdxArr.push(curIdx); length--; curIdx = curIdx === length ? curIdx-1 : curIdx; } if(command === 'Z') { compare[savedIdxArr[savedIdxArr.length-1]] = 'O'; const last = savedIdxArr.pop(); length++; curIdx = last < curIdx ? curIdx+1 : curIdx; } }); return compare.join(''); }
참고 풀이
class Node { constructor(data) { this.next = this.prev = null; this.data = data; } } class LinkedList { constructor() { this.head = this.tail = new Node(); this.size = 0; } pushBack(newNode) { this.pushAt(this.tail, newNode); } pushAt(curNode, newNode) { const nextNode = curNode.next; curNode.next = newNode; newNode.prev = curNode; newNode.next = nextNode; if (nextNode) { nextNode.prev = newNode; } if (curNode === this.tail) { this.tail = newNode; } this.size++; } remove(curNode) { const prevNode = curNode.prev; const nextNode = curNode.next; prevNode.next = nextNode; if (nextNode) { nextNode.prev = prevNode; } if (curNode === this.tail) { this.tail = prevNode; } this.size--; return nextNode == null ? this.tail : nextNode; } print() { let curNode = this.head.next; let str = ""; while (curNode) { str += `${curNode.data} `; curNode = curNode.next; } console.log(str); } } function solution(n, k, cmd) { const linkedList = new LinkedList(); const history = []; for(let i=0; i<n; i++) { const newNode = new Node(i); linkedList.pushBack(newNode); } let curNode = down(linkedList.head.next, k); cmd.forEach(item => { const [command, cnt] = item.split(' '); switch(command) { case 'U' : curNode = up(curNode, +cnt); break; case 'D' : curNode = down(curNode, +cnt); break; case 'C' : curNode = remove(curNode, history, linkedList); break; case 'Z' : restore(history, linkedList); break; } }); const board = Array(n).fill('X') ; curNode = linkedList.head.next; while(curNode) { board[curNode.data] = 'O'; curNode = curNode.next; } return board.join(''); } function up(curNode, cnt) { while(cnt--) { curNode = curNode.prev; } return curNode; } function down(curNode, cnt) { while(cnt--) { curNode = curNode.next; } return curNode; } function remove(curNode, history, linkedList) { history.push([curNode, curNode.prev]); return linkedList.remove(curNode); } function restore(history, linkedList) { const [node, prevNode] = history.pop(); linkedList.pushAt(prevNode, node); }