|
1 | 1 | --- |
2 | 2 | title: 백준 Baekjoon 알고리즘 문제풀이 팁 for NodeJS, JavaScript |
3 | | -createdAt: 2026-01-22 |
| 3 | +createdAt: 2026-01-28 |
4 | 4 | category: ProblemSolving |
5 | 5 | description: NodeJS 환경에서 Baekjoon 온라인 저지 문제를 풀때, 자료구조나 입력/출력 처리가 익숙하지 않은 분들을 위해 유용한 팁들을 정리했습니다. |
6 | 6 | comment: true |
@@ -43,7 +43,7 @@ let input = fs |
43 | 43 |
|
44 | 44 | ```javascript |
45 | 45 | let input = fs |
46 | | - .readFileSync(process.platform === "linux" ? "/dev/stdin" : 0) |
| 46 | + .readFileSync(process.platform === "linux" ? 0 : "./in.txt") |
47 | 47 | .toString() |
48 | 48 | .trim() |
49 | 49 | .split(/\s+/); // 개행문자와 공백문자 모두를 기준으로 분리 |
@@ -83,7 +83,7 @@ for (let n = 0; n < N; n++) { |
83 | 83 |
|
84 | 84 | # ✍️ 출력 처리 꿀팁 |
85 | 85 |
|
86 | | -## 1. 출력 빠르게 하기 |
| 86 | +## 출력 빠르게 하기 |
87 | 87 |
|
88 | 88 | 가끔 출력이 많은 문제에서 `console.log()`를 여러번 호출하면 시간초과가 발생할 수 있습니다. 이럴 때는 출력값을 배열에 모아두었다가 한 번에 출력하면 시간초과가 발생하지 않습니다. |
89 | 89 |
|
@@ -220,6 +220,7 @@ class Queue { |
220 | 220 | this.q[this.tail++] = v; |
221 | 221 | } |
222 | 222 | pop() { |
| 223 | + if (this.isEmpty()) return null; |
223 | 224 | const v = this.q[this.head]; |
224 | 225 | delete this.q[this.head++]; |
225 | 226 | return v; |
@@ -255,11 +256,13 @@ class Deque { |
255 | 256 | this.dq[this.tail++] = v; |
256 | 257 | } |
257 | 258 | popFront() { |
| 259 | + if (this.isEmpty()) return null; |
258 | 260 | const v = this.dq[this.head]; |
259 | 261 | delete this.dq[this.head++]; |
260 | 262 | return v; |
261 | 263 | } |
262 | 264 | popBack() { |
| 265 | + if (this.isEmpty()) return null; |
263 | 266 | const v = this.dq[--this.tail]; |
264 | 267 | delete this.dq[this.tail]; |
265 | 268 | return v; |
@@ -303,7 +306,7 @@ class MaxHeap { |
303 | 306 | } |
304 | 307 | } |
305 | 308 | pop() { |
306 | | - if (this.heap.length === 1) return 0; |
| 309 | + if (this.heap.length === 1) return null; |
307 | 310 | if (this.heap.length === 2) return this.heap.pop(); |
308 | 311 |
|
309 | 312 | const element = this.heap[1]; |
@@ -625,7 +628,7 @@ const char = String.fromCharCode(65); // 'A' |
625 | 628 |
|
626 | 629 | 'A' 는 65, 'a' 는 97, '0' 은 48 의 아스키코드 값을 가집니다. |
627 | 630 |
|
628 | | -문자의 범위를 비교할때 `/\/[A-Z]/` 같은 정규표현식 대신 아스키코드 값을 비교하는 방법이 더 빠릅니다. |
| 631 | +문자의 범위를 비교할때 `/[A-Z]/` 같은 정규표현식 대신 아스키코드 값을 비교하는 방법이 더 빠릅니다. |
629 | 632 |
|
630 | 633 | ```javascript |
631 | 634 | const A = "A".charCodeAt(0); |
@@ -661,10 +664,6 @@ for (...) chars.push(x); |
661 | 664 | let str = chars.join(''); |
662 | 665 | ``` |
663 | 666 |
|
664 | | -```javascript |
665 | | - |
666 | | -``` |
667 | | - |
668 | 667 | <br><br> |
669 | 668 |
|
670 | 669 | # ✍️ 정렬 |
@@ -737,7 +736,7 @@ JSON.stringify({ key: NaN }); // '{"key":null}' |
737 | 736 | JSON.stringify({ key: Infinity }); // '{"key":null}' |
738 | 737 | ``` |
739 | 738 |
|
740 | | -### 비트마스킹시 주의사항 |
| 739 | +## 비트마스킹시 주의사항 |
741 | 740 |
|
742 | 741 | 비트마스킹을 사용하면 집합을 효율적으로 표현하고, 비트 연산을 통해 빠르게 집합 연산을 수행할 수 있습니다. |
743 | 742 |
|
@@ -773,3 +772,9 @@ let mask = 0n; |
773 | 772 |
|
774 | 773 | mask |= 1n << 40n; |
775 | 774 | ``` |
| 775 | + |
| 776 | +<br><br><br><br> |
| 777 | + |
| 778 | +UnionFind, SegmentTree, Trie 와 같은 고급 자료구조나 직접적인 알고리즘들은 따로 다루지 않았습니다. <br/> |
| 779 | +다른 팁들이 생기면 계속 추가할 예정입니다. 😊 <br/> |
| 780 | +추가하면 좋을 내용이 있다면 댓글로 알려주세요 🙌 |
0 commit comments