-
항해 99 5기 TIL_51항해 99 2022. 3. 2. 14:52
▶ Today I Learned
<알고리즘>
[제로]
소요시간: 47분 30초 (문제푸는 데는 15분)
해결여부:
백준 10773번
https://www.acmicpc.net/problem/10773
const fs = require("fs") let input = fs.readFileSync('/dev/stdin').toString().trim().split('\n'); let stack = [] for (let i = 1 ; i < input.length ; i++) { if (input[i]/1 === 0) { stack.pop() } else { stack.push(input[i]/1) } } let answer = stack.reduce((a, b) => a + b) console.log(answer)
분명 풀이는 맞는데 왜 안되는 거지..
그러던 중 구글링 결과 누군가 똑같은 문제를 reduce대신 for문을 돌려 제출한 것을 보았다.
똑같이 해본 결과 통과되었다..!
const input = require("fs").readFileSync('/dev/stdin').toString().trim().split('\n'); let stack = [] let i = 1 while(i < input.length) { if (input[i]/1 === 0) { stack.pop() } else { stack.push(input[i]/1) } i++ } let answer = 0 for (let i = 0; i < stack.length; i++) { answer += stack[i]; } console.log(answer)
그동안 reduce를 이용한 합이 for문이나 while문 보다 시간 복잡도가 적을 것이라 생각했는데 그렇지 않나 보다.
reduce의 시간 복잡도에 대해서 'for문 reduce 시간 복잡도', 'reduce() time complexity'라는 키워드로 검색해보았으나 상위 노출 결과에서 원하는 결과를 얻지 못했다. 시간 복잡도에 대해서 다시 검색해보아야 겠다.
일부 글에서는 둘의 시간 복잡도가 똑같다는 뉘앙스의 의견도 있었다.
출처:
https://stackoverflow.com/questions/42400491/what-is-the-big-o-of-neo4js-reduce-fuction
<실전 프로젝트>
[자바스크립트]
preventDefault(): 공식문서에 따르면 어떤 이벤트를 명시적으로 처리하지 않은 경우, 해당 이벤트에 대한 기본 동작을 실행시키지 않음
https://developer.mozilla.org/ko/docs/Web/API/Event/preventDefault
querySelector(): 괄호 안에 제공된 선택자와 일치하는 문서 내 첫번 째 Element를 반환해줌
https://developer.mozilla.org/ko/docs/Web/API/Document/querySelector
addEventListener(): 지정한 유형의 이벤트를 대상이 수신할 때마다 호출할 함수를 설정함
https://developer.mozilla.org/ko/docs/Web/API/EventTarget/addEventListener
Vanilla Javascript: 프레임워크 또는 라이브러리가 적용되지 않은 순수한 자바스크립트
[Websocket]
Websocket을 사용하여 채팅을 구현하는 도중 서버가 자꾸 연결되지 않았다.
원인을 찾으려 강의의 소스코드와 비교하던 중 다음의 코드가 빠진 것을 발견하였다.
server.listen(3000, handleListen);
아래의 링크에 따르면 .listen이라는 것은 특정 포트와 연결하는데 쓰이는 것이다.
앞으로는 연결이 되지 않을 때 해당 코드가 있는지부터 살펴보아야 겠다.
https://www.geeksforgeeks.org/express-js-app-listen-function/
굳이 String으로 보내주는 이유?
function makeMessage(type, payload) { const msg = { type, payload }; return JSON.stringify(msg); }
클라이언트에서 서버로 메시지나 닉네임 데이터를 보낼 때는 Javascript Object로 보내면 안된다.
이는 서버가 Java나 Go와 같이 다른 언어로 만들어진 경우가 있을 수 있기 때문이다.
[socket.io]
websocket은 socket.io의 하나의 수단일 뿐이며 socket.io는 websocket의 부가기능이 아니다.
실시간, 양방향, 이벤트 기반 소통을 위한 프레임워크이다.
특정 브라우저가 websocket을 지원하지 않아도 socket.io는 http long pollling과 같은 방법으로 바꾸어
계속 작동해나간다.
▶ 느낀 점
오늘 회의 결과 내가 공부해야할 것은 Web RTC, socket.io, mysql, sequalize, MVC 패턴이었다.
...와우 할게 참 많은 걸...? 시간이 좀 빠듯하긴 하지만 그만큼 난 멋진 개발자가 될 수 있지 않을까 생각해본다..ㅎㅎ
호기심을 자극하는 것들이 많다는 건 때론 피곤하게 느껴질 수도 있다.
하지만 시간이 지나면서 성장해가는 내 모습을 보는 것도 참 뿌듯한 일이다.
내일의 내가 더 뿌듯할 수 있게 오늘의 나에게 최선을 다한 하루였길 바란다.
▶ 공부 시 참고 링크들
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/switch#using_switch
https://hanch-dev.tistory.com/4
'항해 99' 카테고리의 다른 글
항해 99 5기 TIL_53 (0) 2022.03.04 항해 99 5기 TIL_52 (0) 2022.03.03 항해 99 5기 TIL_50 (0) 2022.03.01 항해 99 5기 WIL_7 (0) 2022.02.27 항해 99 5기 TIL_49 (0) 2022.02.27