-
-
Notifications
You must be signed in to change notification settings - Fork 335
[reeseo3o] WEEK 09 Solutions #2582
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
+71
−0
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,22 @@ | ||
| // Time Complexity: O(n) | ||
| // Space Complexity: O(1) | ||
|
|
||
| const hasCycle = (head) => { | ||
| if (head === null || head.next === null) { | ||
| return false; | ||
| } | ||
|
|
||
| let slow = head; | ||
| let fast = head.next; | ||
|
|
||
| while (slow !== fast) { | ||
| if (fast === null || fast.next === null) { | ||
| return false; | ||
| } | ||
| slow = slow.next; | ||
| fast = fast.next.next; | ||
| } | ||
|
|
||
| return true; | ||
| }; | ||
|
|
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 각 셀에서 DFS를 수행하는 대신, 해안에서부터 역방향으로 탐색하여 도달 가능한 셀을 표시한다. 전체적으로 각 셀은 한 번씩 방문되므로 시간과 공간 모두 맵 크기에 비례한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
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,49 @@ | ||
| // Time Complexity: O(m * n) | ||
| // Space Complexity: O(m * n) | ||
|
|
||
| const pacificAtlantic = (heights) => { | ||
| if (!heights?.length || !heights[0]?.length) { | ||
| return []; | ||
| } | ||
|
|
||
| const m = heights.length; | ||
| const n = heights[0].length; | ||
| const pacific = Array.from({ length: m }, () => Array(n).fill(false)); | ||
| const atlantic = Array.from({ length: m }, () => Array(n).fill(false)); | ||
| const dirs = [ | ||
| [-1, 0], | ||
| [1, 0], | ||
| [0, -1], | ||
| [0, 1], | ||
| ]; | ||
|
|
||
| const dfs = (visited, r, c) => { | ||
| visited[r][c] = true; | ||
| for (const [dr, dc] of dirs) { | ||
| const nr = r + dr; | ||
| const nc = c + dc; | ||
| if (nr < 0 || nr >= m || nc < 0 || nc >= n || visited[nr][nc]) continue; | ||
| if (heights[nr][nc] < heights[r][c]) continue; | ||
| dfs(visited, nr, nc); | ||
| } | ||
| }; | ||
|
|
||
| for (let i = 0; i < m; i++) { | ||
| dfs(pacific, i, 0); | ||
| dfs(atlantic, i, n - 1); | ||
| } | ||
| for (let j = 0; j < n; j++) { | ||
| dfs(pacific, 0, j); | ||
| dfs(atlantic, m - 1, j); | ||
| } | ||
|
|
||
| const result = []; | ||
| for (let r = 0; r < m; r++) { | ||
| for (let c = 0; c < n; c++) { | ||
| if (pacific[r][c] && atlantic[r][c]) { | ||
| result.push([r, c]); | ||
| } | ||
| } | ||
| } | ||
| 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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 빠른 포인터와 느린 포인터를 동시에 이동시키며, 만나는 지점이 있으면 사이클이 존재하는 것으로 판단한다. 공간은 상수만 사용한다.
개선 제안: 현재 구현이 적절해 보입니다.