From 19ee12bf1f1c6c454b571bcbe5d1ca63010bd01c Mon Sep 17 00:00:00 2001 From: JooKangSan Date: Thu, 9 Jan 2025 14:58:00 +0900 Subject: [PATCH 01/13] =?UTF-8?q?fix:=20=EC=97=90=EB=9F=AC=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../[week1]_Array/ Number_of_duplicate_numbers.js | 0 .../[week1]_Array/ Remove_smallest_number.js | 0 .../[week1]_Array/Array_of_numbers_that_fall_apart.js | 0 .../[week1]_Array/Length_of_elements_in_array.js | 0 .../[week1]_Array/Matrix_addition.js | 0 .../Rotate_array.js" => JooKangSan/[week1]_Array/Rotate_array.js | 0 .../Trim_Array.js" => JooKangSan/[week1]_Array/Trim_Array.js | 0 .../[week1]_Array/\353\260\260\354\227\264.md" | 0 8 files changed, 0 insertions(+), 0 deletions(-) rename "JooKangSan/[week1]\bArray/ Number_of_duplicate_numbers.js" => JooKangSan/[week1]_Array/ Number_of_duplicate_numbers.js (100%) rename "JooKangSan/[week1]\bArray/ Remove_smallest_number.js" => JooKangSan/[week1]_Array/ Remove_smallest_number.js (100%) rename "JooKangSan/[week1]\bArray/Array_of_numbers_that_fall_apart.js" => JooKangSan/[week1]_Array/Array_of_numbers_that_fall_apart.js (100%) rename "JooKangSan/[week1]\bArray/Length_of_elements_in_array.js" => JooKangSan/[week1]_Array/Length_of_elements_in_array.js (100%) rename "JooKangSan/[week1]\bArray/Matrix_addition.js" => JooKangSan/[week1]_Array/Matrix_addition.js (100%) rename "JooKangSan/[week1]\bArray/Rotate_array.js" => JooKangSan/[week1]_Array/Rotate_array.js (100%) rename "JooKangSan/[week1]\bArray/Trim_Array.js" => JooKangSan/[week1]_Array/Trim_Array.js (100%) rename "JooKangSan/[week1]\bArray/\353\260\260\354\227\264.md" => "JooKangSan/[week1]_Array/\353\260\260\354\227\264.md" (100%) diff --git "a/JooKangSan/[week1]\bArray/ Number_of_duplicate_numbers.js" b/JooKangSan/[week1]_Array/ Number_of_duplicate_numbers.js similarity index 100% rename from "JooKangSan/[week1]\bArray/ Number_of_duplicate_numbers.js" rename to JooKangSan/[week1]_Array/ Number_of_duplicate_numbers.js diff --git "a/JooKangSan/[week1]\bArray/ Remove_smallest_number.js" b/JooKangSan/[week1]_Array/ Remove_smallest_number.js similarity index 100% rename from "JooKangSan/[week1]\bArray/ Remove_smallest_number.js" rename to JooKangSan/[week1]_Array/ Remove_smallest_number.js diff --git "a/JooKangSan/[week1]\bArray/Array_of_numbers_that_fall_apart.js" b/JooKangSan/[week1]_Array/Array_of_numbers_that_fall_apart.js similarity index 100% rename from "JooKangSan/[week1]\bArray/Array_of_numbers_that_fall_apart.js" rename to JooKangSan/[week1]_Array/Array_of_numbers_that_fall_apart.js diff --git "a/JooKangSan/[week1]\bArray/Length_of_elements_in_array.js" b/JooKangSan/[week1]_Array/Length_of_elements_in_array.js similarity index 100% rename from "JooKangSan/[week1]\bArray/Length_of_elements_in_array.js" rename to JooKangSan/[week1]_Array/Length_of_elements_in_array.js diff --git "a/JooKangSan/[week1]\bArray/Matrix_addition.js" b/JooKangSan/[week1]_Array/Matrix_addition.js similarity index 100% rename from "JooKangSan/[week1]\bArray/Matrix_addition.js" rename to JooKangSan/[week1]_Array/Matrix_addition.js diff --git "a/JooKangSan/[week1]\bArray/Rotate_array.js" b/JooKangSan/[week1]_Array/Rotate_array.js similarity index 100% rename from "JooKangSan/[week1]\bArray/Rotate_array.js" rename to JooKangSan/[week1]_Array/Rotate_array.js diff --git "a/JooKangSan/[week1]\bArray/Trim_Array.js" b/JooKangSan/[week1]_Array/Trim_Array.js similarity index 100% rename from "JooKangSan/[week1]\bArray/Trim_Array.js" rename to JooKangSan/[week1]_Array/Trim_Array.js diff --git "a/JooKangSan/[week1]\bArray/\353\260\260\354\227\264.md" "b/JooKangSan/[week1]_Array/\353\260\260\354\227\264.md" similarity index 100% rename from "JooKangSan/[week1]\bArray/\353\260\260\354\227\264.md" rename to "JooKangSan/[week1]_Array/\353\260\260\354\227\264.md" From c5da3ed4964b24e39eaf6d993ba8a253ba365cf9 Mon Sep 17 00:00:00 2001 From: JooKangSan Date: Sun, 12 Jan 2025 18:30:49 +0900 Subject: [PATCH 02/13] =?UTF-8?q?=EB=AC=B8=EC=9E=90=EC=97=B4=20=EB=92=A4?= =?UTF-8?q?=EC=A7=91=EA=B8=B0=20/=20=EA=B8=B0=EC=B4=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JooKangSan/[week2]stack/Reverse_String.js | 3 + JooKangSan/[week2]stack/stack.md | 214 ++++++++++++++++++++++ 2 files changed, 217 insertions(+) create mode 100644 JooKangSan/[week2]stack/Reverse_String.js create mode 100644 JooKangSan/[week2]stack/stack.md diff --git a/JooKangSan/[week2]stack/Reverse_String.js b/JooKangSan/[week2]stack/Reverse_String.js new file mode 100644 index 0000000..a6a12aa --- /dev/null +++ b/JooKangSan/[week2]stack/Reverse_String.js @@ -0,0 +1,3 @@ +function solution(my_string) { + return my_string.split('').reverse().join(''); +} \ No newline at end of file diff --git a/JooKangSan/[week2]stack/stack.md b/JooKangSan/[week2]stack/stack.md new file mode 100644 index 0000000..d77c1cb --- /dev/null +++ b/JooKangSan/[week2]stack/stack.md @@ -0,0 +1,214 @@ +# JavaScript Stack 알고리즘 + +## 1. 스택의 기본 구현 +```javascript +class Stack { + constructor() { + this.items = []; + this.count = 0; + } + + // 요소 추가 + push(element) { + this.items[this.count] = element; + this.count++; + return this.count - 1; + } + + // 요소 제거 + pop() { + if(this.count === 0) return undefined; + + this.count--; + const result = this.items[this.count]; + delete this.items[this.count]; + return result; + } + + // 최상단 요소 확인 + peek() { + return this.items[this.count - 1]; + } + + // 스택이 비어있는지 확인 + isEmpty() { + return this.count === 0; + } + + // 스택 크기 반환 + size() { + return this.count; + } + + // 스택 초기화 + clear() { + this.items = []; + this.count = 0; + return this; + } +} +``` + +## 2. 스택을 활용한 알고리즘 예시 + +### 2.1 괄호 매칭 확인 +```javascript +function isValidParentheses(str) { + const stack = []; + const brackets = { + ')': '(', + '}': '{', + ']': '[' + }; + + for (let char of str) { + if (!brackets[char]) { + stack.push(char); + } else { + if (stack.pop() !== brackets[char]) return false; + } + } + + return stack.length === 0; +} +``` + +### 2.2 후위 표기식 계산 +```javascript +function evaluatePostfix(expr) { + const stack = []; + + for(let token of expr.split(' ')) { + if(!isNaN(token)) { + stack.push(Number(token)); + } else { + const b = stack.pop(); + const a = stack.pop(); + + switch(token) { + case '+': stack.push(a + b); break; + case '-': stack.push(a - b); break; + case '*': stack.push(a * b); break; + case '/': stack.push(a / b); break; + } + } + } + + return stack[0]; +} +``` + +### 2.3 히스토리 관리 (실행 취소/재실행) +```javascript +class UndoRedoStack { + constructor() { + this.undoStack = []; + this.redoStack = []; + } + + // 작업 수행 + doAction(action) { + this.undoStack.push(action); + this.redoStack = []; // 새 작업 시 redo 스택 초기화 + } + + // 실행 취소 + undo() { + if (this.undoStack.length === 0) return null; + const action = this.undoStack.pop(); + this.redoStack.push(action); + return action; + } + + // 재실행 + redo() { + if (this.redoStack.length === 0) return null; + const action = this.redoStack.pop(); + this.undoStack.push(action); + return action; + } +} +``` + +## 3. 시간 복잡도 + +### 기본 연산 +- Push: O(1) +- Pop: O(1) +- Peek: O(1) +- isEmpty: O(1) +- Size: O(1) + +### 메모리 사용 +- 공간 복잡도: O(n), n은 스택에 저장된 요소의 수 + +## 4. 활용 사례 + +### 4.1 함수 호출 스택 +```javascript +function first() { + second(); + console.log('first'); +} + +function second() { + third(); + console.log('second'); +} + +function third() { + console.log('third'); +} + +first(); +// 출력: +// third +// second +// first +``` + +### 4.2 웹 브라우저 방문 기록 +```javascript +class BrowserHistory { + constructor() { + this.history = []; + this.current = -1; + } + + visit(url) { + this.current++; + this.history[this.current] = url; + this.history.length = this.current + 1; + } + + back() { + if (this.current > 0) { + this.current--; + return this.history[this.current]; + } + return null; + } + + forward() { + if (this.current < this.history.length - 1) { + this.current++; + return this.history[this.current]; + } + return null; + } +} +``` + +## 5. 주의사항 + +1. 메모리 관리 + - 스택 크기 제한 고려 + - 메모리 누수 방지를 위한 참조 해제 + +2. 에러 처리 + - 빈 스택에서의 pop 연산 처리 + - 스택 오버플로우 방지 + +3. 성능 최적화 + - 배열 크기 동적 조정 최소화 + - 불필요한 복사 연산 방지 \ No newline at end of file From 488872b9573605a981380b4b061cddee2672c5a8 Mon Sep 17 00:00:00 2001 From: JooKangSan Date: Sun, 12 Jan 2025 19:30:40 +0900 Subject: [PATCH 03/13] =?UTF-8?q?Ctrl=20Z=20/=20=EA=B8=B0=EC=B4=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JooKangSan/[week2]stack/ I_hate_English.js | 21 +++++++++++++++++++++ JooKangSan/[week2]stack/Ctrl_Z.js | 19 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 JooKangSan/[week2]stack/ I_hate_English.js create mode 100644 JooKangSan/[week2]stack/Ctrl_Z.js diff --git a/JooKangSan/[week2]stack/ I_hate_English.js b/JooKangSan/[week2]stack/ I_hate_English.js new file mode 100644 index 0000000..c6937c3 --- /dev/null +++ b/JooKangSan/[week2]stack/ I_hate_English.js @@ -0,0 +1,21 @@ +function solution(numbers) { + const nums = [ + "zero", + "one", + "two", + "three", + "four", + "five", + "six", + "seven", + "eight", + "nine", + ]; + let answer = numbers; + + for (let i = 0; i < nums.length; i++) { + answer = answer.replace(nums[i], i); + } + + return Number(answer); +} diff --git a/JooKangSan/[week2]stack/Ctrl_Z.js b/JooKangSan/[week2]stack/Ctrl_Z.js new file mode 100644 index 0000000..11da481 --- /dev/null +++ b/JooKangSan/[week2]stack/Ctrl_Z.js @@ -0,0 +1,19 @@ +function solution(s) { + let stack = []; + let arr = s.split(" "); + + for(let i = 0; i < arr.length; i++) { + if(arr[i] === "Z") { + stack.pop(); + } else { + stack.push(Number(arr[i])); + } + } + + let sum = 0; + for(let i = 0; i < stack.length; i++) { + sum += stack[i]; + } + + return sum; +} \ No newline at end of file From ee8476f6b800d6ab68bfafe85aa4d541c8aeb595 Mon Sep 17 00:00:00 2001 From: JooKangSan Date: Sun, 12 Jan 2025 19:35:51 +0900 Subject: [PATCH 04/13] =?UTF-8?q?=ED=81=AC=EB=A0=88=EC=9D=B8=20=EC=9D=B8?= =?UTF-8?q?=ED=98=95=EB=BD=91=EA=B8=B0=20/=20=EC=A4=91=EA=B8=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../[week2]stack/Calculating_Strings.js | 18 +++++++++++ JooKangSan/[week2]stack/Crane_Claw_Game.js | 30 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 JooKangSan/[week2]stack/Calculating_Strings.js create mode 100644 JooKangSan/[week2]stack/Crane_Claw_Game.js diff --git a/JooKangSan/[week2]stack/Calculating_Strings.js b/JooKangSan/[week2]stack/Calculating_Strings.js new file mode 100644 index 0000000..55d9149 --- /dev/null +++ b/JooKangSan/[week2]stack/Calculating_Strings.js @@ -0,0 +1,18 @@ +function solution(my_string) { + let arr = my_string.split(' '); + let result = Number(arr[0]); + + for(let i = 1; i < arr.length; i += 2) { + const sign = arr[i]; + const num = Number(arr[i + 1]); + + if(sign === '+') { + result += num; + } + if(sign === '-') { + result -= num; + } + } + + return result; +} \ No newline at end of file diff --git a/JooKangSan/[week2]stack/Crane_Claw_Game.js b/JooKangSan/[week2]stack/Crane_Claw_Game.js new file mode 100644 index 0000000..49b29cb --- /dev/null +++ b/JooKangSan/[week2]stack/Crane_Claw_Game.js @@ -0,0 +1,30 @@ +function solution(board, moves) { + let answer = 0; + let stack = []; + + for(let move of moves) { + // moves 배열의 값을 인덱스로 사용하기 위해 1을 빼줍니다 + let col = move - 1; + + // 해당 열에서 가장 위에 있는 인형을 찾습니다 + for(let row = 0; row < board.length; row++) { + if(board[row][col] !== 0) { + let doll = board[row][col]; + + // 인형을 집었으니 해당 위치를 0으로 변경 + board[row][col] = 0; + + // 스택의 최상단 인형과 같다면 두 인형을 터뜨립니다 + if(stack[stack.length - 1] === doll) { + stack.pop(); + answer += 2; + } else { + stack.push(doll); + } + break; + } + } + } + + return answer; +} \ No newline at end of file From ccfa3632f56048589cfe634f0aebc86a71dfb24a Mon Sep 17 00:00:00 2001 From: JooKangSan Date: Sun, 12 Jan 2025 19:42:25 +0900 Subject: [PATCH 05/13] =?UTF-8?q?=EB=8B=A4=ED=8A=B8=EA=B2=8C=EC=9E=84=20/?= =?UTF-8?q?=20=EC=A4=91=EA=B8=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../[week2]stack/Correct_Parentheses.js | 18 ++++++++++ JooKangSan/[week2]stack/Crane_Claw_Game.js | 6 ---- JooKangSan/[week2]stack/Dart_Game.js | 34 +++++++++++++++++++ 3 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 JooKangSan/[week2]stack/Correct_Parentheses.js create mode 100644 JooKangSan/[week2]stack/Dart_Game.js diff --git a/JooKangSan/[week2]stack/Correct_Parentheses.js b/JooKangSan/[week2]stack/Correct_Parentheses.js new file mode 100644 index 0000000..0163d6e --- /dev/null +++ b/JooKangSan/[week2]stack/Correct_Parentheses.js @@ -0,0 +1,18 @@ +function solution(s){ + const stack = []; + + for (let i = 0 ; i < s.length ; i++) { + if(s[i] == '('){ + stack.push(')'); + }else if(s[i] == '['){ + stack.push(']'); + }else if(s[i] == '{'){ + stack.push('}'); + }else{ + if (s[i] !== stack.pop()) { + return false; + } + } + } + return stack.length === 0; +} diff --git a/JooKangSan/[week2]stack/Crane_Claw_Game.js b/JooKangSan/[week2]stack/Crane_Claw_Game.js index 49b29cb..184d1d5 100644 --- a/JooKangSan/[week2]stack/Crane_Claw_Game.js +++ b/JooKangSan/[week2]stack/Crane_Claw_Game.js @@ -3,18 +3,12 @@ function solution(board, moves) { let stack = []; for(let move of moves) { - // moves 배열의 값을 인덱스로 사용하기 위해 1을 빼줍니다 let col = move - 1; - // 해당 열에서 가장 위에 있는 인형을 찾습니다 for(let row = 0; row < board.length; row++) { if(board[row][col] !== 0) { let doll = board[row][col]; - - // 인형을 집었으니 해당 위치를 0으로 변경 board[row][col] = 0; - - // 스택의 최상단 인형과 같다면 두 인형을 터뜨립니다 if(stack[stack.length - 1] === doll) { stack.pop(); answer += 2; diff --git a/JooKangSan/[week2]stack/Dart_Game.js b/JooKangSan/[week2]stack/Dart_Game.js new file mode 100644 index 0000000..ec15139 --- /dev/null +++ b/JooKangSan/[week2]stack/Dart_Game.js @@ -0,0 +1,34 @@ +function solution(dartResult) { + let scores = []; + let currentNumber = ''; + + for(let i = 0; i < dartResult.length; i++) { + let char = dartResult[i]; + + if(!isNaN(char)) { + currentNumber += char; + } + else if(char === 'S' || char === 'D' || char === 'T') { + let score = Number(currentNumber); + + if(char === 'D') score = Math.pow(score, 2); + if(char === 'T') score = Math.pow(score, 3); + + scores.push(score); + currentNumber = ''; + } + else { + if(char === '*') { + scores[scores.length-1] *= 2; + if(scores.length > 1) { + scores[scores.length-2] *= 2; + } + } + else if(char === '#') { + scores[scores.length-1] *= -1; + } + } + } + + return scores.reduce((acc, cur) => acc + cur, 0); +} \ No newline at end of file From 952ddb992abd3fe49bedb2bde461d2c789162bb7 Mon Sep 17 00:00:00 2001 From: JooKangSan Date: Thu, 16 Jan 2025 20:36:06 +0900 Subject: [PATCH 06/13] =?UTF-8?q?=EC=88=9C=EC=84=9C=EC=8C=8D=EC=9D=98=20?= =?UTF-8?q?=EA=B0=AF=EC=88=98=20/=20=EA=B8=B0=EC=B4=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../[week3]Queue/Number_of_ordered_pairs.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 JooKangSan/[week3]Queue/Number_of_ordered_pairs.js diff --git a/JooKangSan/[week3]Queue/Number_of_ordered_pairs.js b/JooKangSan/[week3]Queue/Number_of_ordered_pairs.js new file mode 100644 index 0000000..fc008d3 --- /dev/null +++ b/JooKangSan/[week3]Queue/Number_of_ordered_pairs.js @@ -0,0 +1,21 @@ +function solution(n) { + let queue = []; + let count = 0; + for(let i = 1; i <= Math.sqrt(n); i++) { + if(n % i === 0) { + queue.push(i); + } + } + while(queue.length > 0) { + const num = queue.shift(); + const pair = n / num; + + if(num === pair) { + count++; + } else { + count += 2; + } + } + + return count; +} \ No newline at end of file From d7e9a27241e19e730afe578f8a844b68760fd3e8 Mon Sep 17 00:00:00 2001 From: JooKangSan Date: Thu, 16 Jan 2025 20:39:26 +0900 Subject: [PATCH 07/13] =?UTF-8?q?=EC=A0=90=EC=9D=98=20=EC=9C=84=EC=B9=98?= =?UTF-8?q?=20=EA=B5=AC=ED=95=98=EA=B8=B0=20/=20=EA=B8=B0=EC=B4=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../[week3]Queue/Find_the_location_of_a_point.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 JooKangSan/[week3]Queue/Find_the_location_of_a_point.js diff --git a/JooKangSan/[week3]Queue/Find_the_location_of_a_point.js b/JooKangSan/[week3]Queue/Find_the_location_of_a_point.js new file mode 100644 index 0000000..070e5e3 --- /dev/null +++ b/JooKangSan/[week3]Queue/Find_the_location_of_a_point.js @@ -0,0 +1,11 @@ +function solution(dot) { + let queue = [...dot] + + const x = queue.shift(); + const y = queue.shift(); + + if(x > 0 && y > 0) return 1; + if(x < 0 && y > 0) return 2; + if(x < 0 && y < 0) return 3; + return 4; +} \ No newline at end of file From 42d696ffdf833b3a79562e279fad3895e77b8b37 Mon Sep 17 00:00:00 2001 From: JooKangSan Date: Thu, 16 Jan 2025 20:41:20 +0900 Subject: [PATCH 08/13] =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EC=84=B1?= =?UTF-8?q?=EA=B3=B5=3F=20/=20=EA=B8=B0=EC=B4=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JooKangSan/[week3]Queue/Success_Login.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 JooKangSan/[week3]Queue/Success_Login.js diff --git a/JooKangSan/[week3]Queue/Success_Login.js b/JooKangSan/[week3]Queue/Success_Login.js new file mode 100644 index 0000000..0c80eef --- /dev/null +++ b/JooKangSan/[week3]Queue/Success_Login.js @@ -0,0 +1,19 @@ +function solution(id_pw, db) { + let queue = [...db]; + + const [id, pw] = id_pw; + let hasId = false; + + while(queue.length > 0) { + const [dbId, dbPw] = queue.shift(); + + if(dbId === id) { + hasId = true; + if(dbPw === pw) { + return "login"; + } + } + } + + return hasId ? "wrong pw" : "fail"; +} \ No newline at end of file From 3ee8e31b30db0ff032ab86adc41773c0691ab51c Mon Sep 17 00:00:00 2001 From: JooKangSan Date: Thu, 16 Jan 2025 20:51:59 +0900 Subject: [PATCH 09/13] =?UTF-8?q?=EC=9D=B4=EC=83=81=ED=95=9C=20=EC=A0=95?= =?UTF-8?q?=EB=A0=AC=20/=20=EA=B8=B0=EC=B4=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../[week3]Queue/Unusual_arrangement.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 JooKangSan/[week3]Queue/Unusual_arrangement.js diff --git a/JooKangSan/[week3]Queue/Unusual_arrangement.js b/JooKangSan/[week3]Queue/Unusual_arrangement.js new file mode 100644 index 0000000..73b26e5 --- /dev/null +++ b/JooKangSan/[week3]Queue/Unusual_arrangement.js @@ -0,0 +1,19 @@ +function solution(numlist, n) { + let queue = [...numlist]; + let result = []; + + queue.sort((a, b) => { + const A = Math.abs(n - a); + const B = Math.abs(n - b); + if(A === B) { + return b - a; + } + return A - B; + }); + + while(queue.length > 0) { + result.push(queue.shift()); + } + + return result; +} \ No newline at end of file From 59443db5732f833fe95c0f545d036d512c256b11 Mon Sep 17 00:00:00 2001 From: JooKangSan Date: Thu, 16 Jan 2025 20:56:32 +0900 Subject: [PATCH 10/13] =?UTF-8?q?=EC=B9=B4=EB=93=9C=20=EB=AD=89=EC=B9=98?= =?UTF-8?q?=20/=20=EA=B8=B0=EC=B4=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JooKangSan/[week3]Queue/Pack_of_Cards.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 JooKangSan/[week3]Queue/Pack_of_Cards.js diff --git a/JooKangSan/[week3]Queue/Pack_of_Cards.js b/JooKangSan/[week3]Queue/Pack_of_Cards.js new file mode 100644 index 0000000..426ff50 --- /dev/null +++ b/JooKangSan/[week3]Queue/Pack_of_Cards.js @@ -0,0 +1,17 @@ +function solution(cards1, cards2, goal) { + let queue1 = [...cards1]; + let queue2 = [...cards2]; + + for(let word of goal) { + if(queue1.length > 0 && word === queue1[0]) { + queue1.shift(); + } + else if(queue2.length > 0 && word === queue2[0]) { + queue2.shift(); + } + else { + return "No"; + } + } + return "Yes"; +} \ No newline at end of file From ad8b945f98d03a529696663bb992fb3ce8c2aa96 Mon Sep 17 00:00:00 2001 From: JooKangSan Date: Thu, 16 Jan 2025 21:13:50 +0900 Subject: [PATCH 11/13] =?UTF-8?q?feat:=20md=20=ED=8C=8C=EC=9D=BC=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JooKangSan/[week3]Queue/Queue.md | 125 +++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 JooKangSan/[week3]Queue/Queue.md diff --git a/JooKangSan/[week3]Queue/Queue.md b/JooKangSan/[week3]Queue/Queue.md new file mode 100644 index 0000000..a3cb658 --- /dev/null +++ b/JooKangSan/[week3]Queue/Queue.md @@ -0,0 +1,125 @@ +# JavaScript Queue Algorithm + +## 1. 큐(Queue)란? + +선입선출 원칙을 따르는 선형 자료구조 => 먼저 들어온 데이터가 먼저 나감 + +### 기본 특성 + +- 데이터는 한쪽 끝(rear)에서만 삽입되고, 다른 한쪽 끝(front)에서만 삭제됨 +- 주로 대기열 관리, 작업 스케줄링 등에 사용 + +## 2. 자바스크립트에서 큐 구현 방법 + +### 배열을 사용한 기본 구현 + +```javascript +// 1. 기본적인 배열 메서드 사용 +let queue = []; + +// 삽입 (enqueue) +queue.push(element); + +// 삭제 (dequeue) +queue.shift(); + +// 맨 앞 요소 확인 (peek) +queue[0]; + +// 큐가 비었는지 확인 +queue.length === 0; +``` + +## 3. 큐의 주요 활용 사례 + +### 3.1 너비 우선 탐색(BFS) + +```javascript +function bfs(graph, start) { + // 방문한 노드 저장 + const visited = new Set(); + + // 시작 노드를 큐에 추가 + const queue = [start]; + + // 큐가 빌 때까지 반복 + while (queue.length > 0) { + // 현재 노드 추출 + const vertex = queue.shift(); + // 방문하지 않은 노드라면 + if (!visited.has(vertex)) { + // 방문 처리 + visited.add(vertex); + // 인접 노드들을 큐에 추가 + queue.push(...graph[vertex]); + } + } + + return visited; +} +``` + +### 3.2 작업 처리 대기열 + +```javascript +function processTaskQueue(tasks) { + // 작업 목록을 큐로 변환 + const queue = [...tasks]; + // 결과 저장 배열 + const results = []; + + // 큐가 빌 때까지 반복 + while (queue.length > 0) { + // 작업 추출 + const task = queue.shift(); + // 작업 처리 + const result = processTask(task); + // 결과 저장 + results.push(result); + } + + return results; +} +``` + +### 3.3 이벤트 처리 + +```javascript +const eventQueue = { + // 이벤트 큐 초기화 + queue: [], + + // 새 이벤트 추가 + addEvent: function (event) { + this.queue.push(event); + }, + + processEvents: function () { + // 큐가 빌 때까지 반복 + while (this.queue.length > 0) { + // 이벤트 추출 + const event = this.queue.shift(); + // 이벤트 처리 + handleEvent(event); + } + }, +}; + +eventQueue.addEvent("event1"); +eventQueue.processEvents(); +``` + +## 4. 성능 고려사항 + +### 4.1 시간 복잡도 + +- Enqueue (삽입): O(1) +- Dequeue (삭제): O(n) - shift() 사용 시 +- Peek (조회): O(1) + +### 4.2 주의사항 + +1. 배열의 shift() 연산은 O(n)의 시간 복잡도를 가짐 +2. 대량의 데이터를 처리할 때는 최적화된 구현 방식 고려 +3. 메모리 관리에 주의 (불필요한 참조 제거) +4. 동시성 처리 시 적절한 동기화 메커니즘 사용 From 3ed157c7d5372d3a7eb5278e6efcd4425e30ded8 Mon Sep 17 00:00:00 2001 From: JooKangSan Date: Thu, 16 Jan 2025 21:30:09 +0900 Subject: [PATCH 12/13] =?UTF-8?q?=EA=B3=B5=EC=9B=90=20=EC=82=B0=EC=B1=85?= =?UTF-8?q?=20/=20=EC=A4=91=EA=B8=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JooKangSan/[week3]Queue/Walk_in_the_park.js | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 JooKangSan/[week3]Queue/Walk_in_the_park.js diff --git a/JooKangSan/[week3]Queue/Walk_in_the_park.js b/JooKangSan/[week3]Queue/Walk_in_the_park.js new file mode 100644 index 0000000..00d710f --- /dev/null +++ b/JooKangSan/[week3]Queue/Walk_in_the_park.js @@ -0,0 +1,29 @@ +function solution(park, routes) { + let queue = [...routes]; + + let [x, y] = park.map((s, i) => [s.indexOf('S'), i]).find(v => v[0] > -1); + + let [h, w] = [park.length, park[0].length]; + + while(queue.length) { + let [d, n] = queue.shift().split(' '); + let [nx, ny] = [x, y]; + let go = true; + + for(let i = 0; i < n; i++) { + if(d === 'N') ny--; + if(d === 'S') ny++; + if(d === 'W') nx--; + if(d === 'E') nx++; + + if(ny < 0 || ny >= h || nx < 0 || nx >= w || park[ny][nx] === 'X') { + go = false; + break; + } + } + + if(go) [x, y] = [nx, ny]; + } + + return [y, x]; +} \ No newline at end of file From 8b2f46fcaba1f2043ca8ebbe6729006c9d10c74a Mon Sep 17 00:00:00 2001 From: JooKangSan Date: Thu, 16 Jan 2025 21:41:25 +0900 Subject: [PATCH 13/13] =?UTF-8?q?=ED=96=84=EB=B2=84=EA=B1=B0=20=EB=A7=8C?= =?UTF-8?q?=EB=93=A4=EA=B8=B0=20/=20=EC=A4=91=EA=B8=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JooKangSan/[week3]Queue/Making_Burgers.js | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 JooKangSan/[week3]Queue/Making_Burgers.js diff --git a/JooKangSan/[week3]Queue/Making_Burgers.js b/JooKangSan/[week3]Queue/Making_Burgers.js new file mode 100644 index 0000000..457549f --- /dev/null +++ b/JooKangSan/[week3]Queue/Making_Burgers.js @@ -0,0 +1,46 @@ +function solution(ingredient) { + let stack = []; + let count = 0; + let i = 0; + + while(i < ingredient.length) { + stack.push(ingredient[i]); + + if(stack.length >= 4) { + const len = stack.length; + if(stack[len-4] === 1 && + stack[len-3] === 2 && + stack[len-2] === 3 && + stack[len-1] === 1) { + stack.splice(-4); + count++; + } + } + i++; + } + + return count; + } + +// function solution(ingredient) { +// let queue = [...ingredient]; +// let stack = []; +// let count = 0; + +// while(queue.length > 0) { +// stack.push(queue.shift()); + +// if(stack.length >= 4) { +// const len = stack.length; +// if(stack[len-4] === 1 && +// stack[len-3] === 2 && +// stack[len-2] === 3 && +// stack[len-1] === 1) { +// stack.splice(-4); +// count++; +// } +// } +// } + +// return count; +// } \ No newline at end of file