-
Notifications
You must be signed in to change notification settings - Fork 4
[JooKangsan] 25.01.16 #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
19ee12b
fix: 에러 수정
JooKangsan 1e18910
Merge main
JooKangsan c5da3ed
문자열 뒤집기 / 기초
JooKangsan 488872b
Ctrl Z / 기초
JooKangsan ee8476f
크레인 인형뽑기 / 중급
JooKangsan ccfa363
다트게임 / 중급
JooKangsan fb13521
Merge main
JooKangsan 952ddb9
순서쌍의 갯수 / 기초
JooKangsan d7e9a27
점의 위치 구하기 / 기초
JooKangsan 42d696f
로그인 성공? / 기초
JooKangsan 3ee8e31
이상한 정렬 / 기초
JooKangsan 59443db
카드 뭉치 / 기초
JooKangsan ad8b945
feat: md 파일 추가
JooKangsan 3ed157c
공원 산책 / 중급
JooKangsan 8b2f46f
햄버거 만들기 / 중급
JooKangsan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| // } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. 동시성 처리 시 적절한 동기화 메커니즘 사용 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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]; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
배열복사를안하고 numlist 를 그대로정렬했는데 복사해서 처리하셔서 신기했습니다!
따로 복사해서 처리하신 이유가있을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이번 문제를 다 복사해서 풀다보니 너무 자연스럽게 복사부터한거같네요... 따로 복사를 안해도 결과는 같을거 같아요!! 이렇게 듣고보니 불필요한 코드인거같아요!! 감사합니다!