-
-
Notifications
You must be signed in to change notification settings - Fork 335
[sadie100] Week 09 Solutions #2576
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
2 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,32 @@ | ||
| /** | ||
| * Definition for singly-linked list. | ||
| * 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) | ||
| * } | ||
| * } | ||
| */ | ||
|
|
||
| /* | ||
| 리스트 노드를 저장하는 Map을 생성, 노드를 탐색하며 해당 Map에 노드를 저장하고 이미 있는 노드일 경우 true 리턴 | ||
|
|
||
| 시간복잡도 : O(N) - N은 리스트의 개수 | ||
| 공간복잡도 : O(N) - Map 객체 | ||
| */ | ||
|
|
||
| function hasCycle(head: ListNode | null): boolean { | ||
| const nodeMap = new Map<ListNode, boolean>() | ||
|
sadie100 marked this conversation as resolved.
|
||
|
|
||
| while (head) { | ||
| if (nodeMap.get(head) === true) { | ||
| return true | ||
| } | ||
| nodeMap.set(head, true) | ||
| head = head.next | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
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,79 @@ | ||
| /** | ||
| pacific에 닿을 수 있는 grid를 판별하는 이중 배열과 atlantic에 닿을 수 있는지를 판별하는 이중 배열을 생성한다. | ||
| pacific 배열은 r, c 중 하나라도 0일 경우 true값, | ||
| atlantic 배열은 r, c 중 하나라도 length값과 같을 경우 true값을 채운다 | ||
|
|
||
| 이후 r, c 탐색을 돌며 pacific/ atlantic true인 값이 있으면 dfs를 돌린다 | ||
| - 이전 heights값보다 크거나 같으면 true 처리, 인접한 셀을 또 dfs 탐색 | ||
|
|
||
| 두 배열값이 둘 다 true인 위치들을 리턴한다 | ||
|
|
||
| 시간복잡도 : O(M*N) - 순회 | ||
| */ | ||
|
|
||
| function pacificAtlantic(heights: number[][]): number[][] { | ||
| const m = heights.length | ||
| const n = heights[0].length | ||
| const result = [] | ||
| const pacificChecker = Array.from({ length: m }).map((el, idx) => { | ||
| if (idx === 0) { | ||
| return new Array(n).fill(true) | ||
| } else { | ||
| const arr = new Array(n).fill(false) | ||
| arr[0] = true | ||
| return arr | ||
| } | ||
| }) | ||
| const atlanticChecker = Array.from({ length: m }).map((el, idx) => { | ||
| if (idx === m - 1) { | ||
| return new Array(n).fill(true) | ||
| } else { | ||
| const arr = new Array(n).fill(false) | ||
| arr[n - 1] = true | ||
| return arr | ||
| } | ||
| }) | ||
|
|
||
| const dx = [0, 1, -1, 0] | ||
| const dy = [1, 0, 0, -1] | ||
|
|
||
| const search = (row, col, checker, stdValue) => { | ||
| const value = heights[row][col] | ||
| if (value < stdValue) return | ||
|
|
||
| checker[row][col] = true | ||
|
|
||
| for (let i = 0; i < 4; i++) { | ||
| const newRow = row + dx[i] | ||
| const newCol = col + dy[i] | ||
| if (newRow < 0 || newRow >= m) continue | ||
| if (newCol < 0 || newCol >= n) continue | ||
| if (checker[newRow][newCol]) continue | ||
|
|
||
| search(newRow, newCol, checker, value) | ||
| } | ||
| } | ||
|
|
||
| // dfs 탐색 | ||
| for (let i = 0; i < m; i++) { | ||
| for (let j = 0; j < n; j++) { | ||
| if (pacificChecker[i][j]) { | ||
| search(i, j, pacificChecker, heights[i][j]) | ||
| } | ||
| if (atlanticChecker[i][j]) { | ||
| search(i, j, atlanticChecker, heights[i][j]) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (let i = 0; i < m; i++) { | ||
| for (let j = 0; j < n; j++) { | ||
| if (pacificChecker[i][j] && atlanticChecker[i][j]) { | ||
| result.push([i, j]) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| //pacific, atlantic 모두 true인 인덱스배열 반환 | ||
| return result | ||
| } |
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.
🏷️ 알고리즘 패턴 분석