-
항해 99 5기 TIL_52항해 99 2022. 3. 3. 03:42
▶ Today I Learned
<알고리즘>
[회전하는 큐]
소요시간: 2시간
해결여부: 미해결
https://www.acmicpc.net/problem/1021
참고 자료
https://tesseractjh.tistory.com/45
https://st-lab.tistory.com/216
https://lhoiktiv.tistory.com/206
초반에 문제 자체를 이해하지 못해서 참고 자료로 답변들을 찾아보았다
그러던 와중 풀이들이 모두 엄청 길다는 것을 알았다..
아래는 그 중 한 답변을 그대로 가져온 것이다.
const array = ['10','3','2','9','5'] const [arr,n, ...m] = array.map(v => +v) let count = 0; function Node(value) { this.value = value; this.prevNode = null; this.nextNode = null; } function DoublyLinkedList() { this.front = null; this.rear = null; this.length = 0; this.enqueue = value => { let curNode = new Node(value); if (this.front) { curNode.prevNode = this.rear; curNode.nextNode = this.front; console.log(curNode) this.rear.nextNode = curNode; this.front.prevNode = curNode; this.rear = curNode; } else { this.front = curNode; this.rear = curNode; curNode.prevNode = curNode; curNode.nextNode = curNode; } this.length++; }; this.poll = () => { if (this.length === 1) { this.front = null; this.rear = null; } else { let secondNode = this.front.nextNode secondNode.prevNode = this.rear; this.rear.nextNode = secondNode; this.front = secondNode; this.length--; } } this.rotateToLeft = (n=1) => { while (n > 0) { if (this.length > 1) { this.rear = this.front; this.front = this.front.nextNode; } n--; } } this.rotateToRight = (n=1) => { while (n > 0) { if (this.length > 1) { this.front = this.rear; this.rear = this.rear.prevNode; } n--; } } this.extract = value => { if (this.length <= 1) { return 0; } else { let curNode = this.front; let leftCount = 0; let rightCount = 0; while (curNode.value !== value) { curNode = curNode.nextNode; leftCount++; } curNode = this.front; while (curNode.value !== value) { curNode = curNode.prevNode; rightCount++; } if (leftCount < rightCount) { this.rotateToLeft(leftCount); this.poll(); return leftCount; } else { this.rotateToRight(rightCount); this.poll(); return rightCount; } } } } dll = new DoublyLinkedList(); for (let i=1; i<=n; i++) dll.enqueue(i); console.log(arr.reduce((acc, v) => acc + dll.extract(v), 0));
어떻게든 이해하려 노력했지만 아직 머리에 잘 들어오지는 않는다...
현재는 const [arr,n, ...m] = array.map(v => +v) 까지만 이해한 상태이다..
그래도 이런 코드도 보고 이해할 수 있도록 해야 성장할 수 있을 것이다.
내일 할 일 하다가 여유가 될 때 다시 뜯어봐야겠다.
<실전 프로젝트>
[socket.io]
socket.emit이라는 코드에서는
1) 어떠한 이벤트도 써줄 수 있으며
2) socket.send()와 다르게 object type도 보내줄 수 있다. (숫자와 같은 다른 데이터 타입을 보내도 되며 데이터를 한 개만 보내야 한다는 제약도 없다.)
socket.send()를 썼을 때에는 데이터를 object 파일 그대로 보내지 않고 반드시 string으로 바꾸어주었다.
이는 서버가 파이썬, 자바 등 어떤 언어 기반인지, 그리고 어떤 형식으로 해당 메시지를 다루는 지 알 수 없었기 때문에 공통적으로 쓰이는 string으로 바꾸는 것이다. 반면 socket.io는 해당 작업을 알아서 해줄 것이다.
3) 클라이언트에 있는 함수를 서버에서 실행명령 내릴 수가 있다.
단, 클라이언트 측의 socket.emit에서 쓰이는 이벤트 이름과 서버 측의 socket.on에서 쓰이는 이름이 같아야 한다.
그리고 socket.emit 내에서 해당 함수를 가장 마지막에 위치시켜야 한다.
()안을 보자면 첫번째는 이벤트 명, 두 번째 부터는 데이터들, 마지막은 해당 함수가 나오는 식이다.
클라이언트 측 코드
function backendDone(msg) { console.log('이게 가능하다구요?'); } ... socket.emit("enter_room", input.value, backendDone); input.value = "";
서버 측 코드
socket.on("enter_room", (roomName, done) => { console.log(roomName); setTimeout(() => { done("실행은 백엔드에서 시켰지만 실행되는 건 프론트엔드에서이다."); }, 10000); });
참고: https://nomadcoders.co/noom/lobby
▶ 느낀 점
오늘도 맡은 알고리즘을 제대로 풀어내지 못했다.
이때까지 적용해보지 못한 패턴들과 식들이었기에 끝끝내 풀어내지 못했던 자신에게
자괴감이 들기도 했다.
하지만 꾸준히 계단을 밟아나가다 보면 이 정도 문제는 가볍게 풀어내는 자신을 발견하게 되지 않을까하고
기대해본다. 이때까지 많은 공부들이 그래왔듯이.
지금은 힘들지만 언젠가 할 수 있을 것이라 믿는다.
오늘도 고생많았다 내 자신 :)
▶ 공부 시 참고 링크들
https://m.blog.naver.com/tkdldjs35/221591357991
https://nomadcoders.co/noom/lobby
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
'항해 99' 카테고리의 다른 글
항해 99 5기 TIL_54 (0) 2022.03.05 항해 99 5기 TIL_53 (0) 2022.03.04 항해 99 5기 TIL_51 (0) 2022.03.02 항해 99 5기 TIL_50 (0) 2022.03.01 항해 99 5기 WIL_7 (0) 2022.02.27