-
-
Notifications
You must be signed in to change notification settings - Fork 305
[Blossssom] WEEK 09 solutions #2264
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
4 commits
Select commit
Hold shift + click to select a range
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,53 @@ | ||
| class ListNode { | ||
| val: number; | ||
| next: ListNode | null; | ||
| constructor(val?: number, next?: ListNode | null) { | ||
| this.val = val === undefined ? 0 : val; | ||
| this.next = next === undefined ? null : next; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param head - linked list node | ||
| * @returns - 순환되는 리스트인지? | ||
| * @description | ||
| * - 풀이 1. - 단순 순회 및 조회로 판단, 결국 visit에 다시 돌아온다면 순환 | ||
| * - 풀이 2. - 1칸, 2칸 포인터를 나눠 결국 순환이라면 돌고 돌아 만나는 과정을 통한 메모리 O(1)의 풀이 | ||
| */ | ||
|
|
||
| // function hasCycle(head: ListNode | null): boolean { | ||
| // const visit = new Set(); | ||
| // let current = head; | ||
|
|
||
| // while (current) { | ||
| // if (visit.has(current)) { | ||
| // return true; | ||
| // } | ||
|
|
||
| // visit.add(current); | ||
| // current = current.next; | ||
| // } | ||
|
|
||
| // return false; | ||
| // } | ||
|
|
||
| function hasCycle(head: ListNode | null): boolean { | ||
| if (!head || !head.next) { | ||
| return false; | ||
| } | ||
|
|
||
| let slow: ListNode | null = head; | ||
| let fast: ListNode | null = head; | ||
|
|
||
| while (fast && fast.next) { | ||
| slow = slow!.next; | ||
| fast = fast.next.next; | ||
|
|
||
| if (slow === fast) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
|
|
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,37 @@ | ||
| /** | ||
| * @param nums - 정수 배열 | ||
| * @returns - 정수 배열의 부분 배열의 곱이 가장 큰 값을 반환 | ||
| * @description | ||
| * - 값이 음수일 경우 최솟값과 곱해 최댓값을 도출 할 수 있으므로 swap | ||
| * - min, max를 현재 값과 비교하며 변경 | ||
| */ | ||
|
|
||
| function maxProduct(nums: number[]): number { | ||
| let maximum = nums[0]; | ||
| let minimum = nums[0]; | ||
| let result = nums[0]; | ||
|
|
||
| for (let i = 1; i < nums.length; i++) { | ||
| const current = nums[i]; | ||
|
|
||
| if (current < 0) { | ||
| let temp = maximum; | ||
| maximum = minimum; | ||
| minimum = temp; | ||
| } | ||
|
|
||
| maximum = Math.max(current, current * maximum); | ||
| minimum = Math.min(current, current * minimum); | ||
|
|
||
| result = Math.max(result, maximum); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| const nums = [-2, 3, -4]; | ||
|
|
||
| maxProduct(nums); | ||
|
|
||
|
|
||
|
|
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,89 @@ | ||
| /** | ||
| * @param heights - 셀의 해발고도 배열 | ||
| * @returns | ||
| * @description | ||
| * - 인접한 셀의 높이가 작거나 같으면 빗물이 흐를 수 있음 | ||
| * - 바다에 인접한 셀에서는 바다로 물이 흐를 수 있음 | ||
| * - 태평양과 대서양 모두로 흐를 수 있는 경로 반환 | ||
| * - 결국 [0, n] 과 [m, 0]는 반드시 포함 | ||
| * | ||
| * 1. 역순으로 가는게 유리하다는 힌트를 채택 | ||
| * 2. 모든 셀의 이동 가능 방향을 파악 (dfs, bfs) | ||
| */ | ||
|
|
||
| function pacificAtlantic(heights: number[][]): number[][] { | ||
| const yMax = heights.length; | ||
| const xMax = heights[0].length; | ||
|
|
||
| // 태평양, 대서양의 visit 체크를 위한 set | ||
| const pacific = new Set<string>(); | ||
| const atlantic = new Set<string>(); | ||
|
|
||
| const result: number[][] = []; | ||
| function dfs(y: number, x: number, visited: Set<string>, prevHeight: number) { | ||
| const key = `${y},${x}`; | ||
|
|
||
| if ( | ||
| x < 0 || | ||
| x >= xMax || | ||
| y < 0 || | ||
| y >= yMax || | ||
| visited.has(key) || | ||
| prevHeight > heights[y][x] | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| visited.add(key); | ||
| dfs(y, x + 1, visited, heights[y][x]); | ||
| dfs(y, x - 1, visited, heights[y][x]); | ||
| dfs(y + 1, x, visited, heights[y][x]); | ||
| dfs(y - 1, x, visited, heights[y][x]); | ||
| } | ||
|
|
||
| // 맨 양 끝쪽을 dfs의 시작점으로 지정 | ||
| for (let i = 0; i < yMax; i++) { | ||
| dfs(i, 0, pacific, heights[i][0]); | ||
| dfs(i, xMax - 1, atlantic, heights[i][xMax - 1]); | ||
| } | ||
|
|
||
| for (let j = 0; j < xMax; j++) { | ||
| dfs(0, j, pacific, heights[0][j]); | ||
| dfs(yMax - 1, j, atlantic, heights[yMax - 1][j]); | ||
| } | ||
|
|
||
| // 유니온 값 체크 | ||
| for (let i = 0; i < yMax; i++) { | ||
| for (let j = 0; j < xMax; j++) { | ||
| const key = `${i},${j}`; | ||
|
|
||
| if (pacific.has(key) && atlantic.has(key)) { | ||
| result.push([i, j]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| const heights = [ | ||
| [1, 2, 2, 3, 5], | ||
| [3, 2, 3, 4, 4], | ||
| [2, 4, 5, 3, 1], | ||
| [6, 7, 1, 4, 5], | ||
| [5, 1, 1, 2, 4], | ||
| ]; | ||
|
|
||
| pacificAtlantic(heights); | ||
|
|
||
| // 태 평 양 (Pacific) | ||
| // ~ ~ ~ ~ ~ ~ | ||
| // ~ [1] [2] [2] [3] (5) ~ <-- 오른쪽/아래는 대서양 | ||
| // ~ [3] [2] [3] (4) (4) ~ | ||
| // ~ [2] [4] (5) [3] [1] ~ | ||
| // ~ (6) (7) (1) [4] [5] ~ | ||
| // ~ (5) [1] [1] [2] [4] ~ | ||
| // ~ ~ ~ ~ ~ ~ | ||
| // 대 서 양 (Atlantic) | ||
|
|
||
|
|
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,28 @@ | ||
| /** | ||
| * | ||
| * @param a - 정수 a | ||
| * @param b - 정수 b | ||
| * @returns - 더하기 연산 없이 더한 값 반환 | ||
| * @description | ||
| * - bit 연산이구나! 싶었는데 비트를 잘 다룰줄 몰라서 ai의 도움을 받음 | ||
| */ | ||
|
|
||
| function getSum(a: number, b: number): number { | ||
| while (b !== 0) { | ||
| // 올림수 계산 : 둘다 1인 비트를 AND 연산으로 찾아 왼쪽으로 밈 (올림수니까) | ||
| const carry = (a & b) << 1; | ||
|
|
||
| // XOR 연산으로 올림수를 제외한 덧셈 값 도출 (1 | 0 이므로) | ||
| a = a ^ b; | ||
|
|
||
| b = carry; | ||
| } | ||
|
|
||
| return a; | ||
| } | ||
|
|
||
| const a = 2; | ||
| const b = 3; | ||
| getSum(a, b); | ||
|
|
||
|
|
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Floyd 알고리즘 선택해서 공간 O(1)로 풀이하셨네요! 👍