[freemjstudio] WEEK 10 Solutions - #2845
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
๐ท๏ธ ์๊ณ ๋ฆฌ์ฆ ํจํด ๋ถ์
course-schedule/freemjstudio.py
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
# cycle ์ ๊ฐ์งํ๋ฉด false ๋ฅผ ๋ฆฌํดํ๋ค.
graph = [[] for _ in range(numCourses)]
# a <- b
for a, b in prerequisites:
graph[b].append(a)
state = [0] * numCourses
# 0: unvisited 1: visiting 2: done
def dfs(node):
if state[node] == 1:
return False
if state[node] == 2:
return True
state[node] = 1 # visiting
for next_node in graph[node]:
if dfs(next_node) == False:
return False
state[node] = 2 # done
return True
for course in range(numCourses):
if dfs(course) == False:
return False
return True- ํจํด: Depth-First Search, Graph
- ์ค๋ช : ๊ทธ๋ํ๋ฅผ ๊ตฌ์ฑํ๊ณ DFS๋ก ์ฌ์ดํด ์ฌ๋ถ๋ฅผ ํ์งํ๋ ํจํด์ผ๋ก, ๋ฐฉ๋ฌธ ์ํ๋ฅผ ์ถ์ ํด ์ํ ์ฌ๋ถ๋ฅผ ํ๋จํ๋ค. ์ฌ์ดํด์ด ์์ผ๋ฉด false, ์์ผ๋ฉด true๋ฅผ ๋ฐํํ๋ค.
๐ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์
| ๋ณต์ก๋ | |
|---|---|
| Time | O(N + P) |
| Space | O(N + P) |
ํผ๋๋ฐฑ: ๊ทธ๋ํ๋ฅผ ์ธ์ ๋ฆฌ์คํธ๋ก ๊ตฌ์ฑํ๊ณ ๊ฐ ๋ ธ๋๋ฅผ DFSํ๋ฉฐ ์ฌ์ดํด ์ฌ๋ถ๋ฅผ ๊ฒ์ฌํ๋ค. ์ํ ๋ฐฐ์ด๋ก ๋ฐฉ๋ฌธ ์ค์ธ ๋ ธ๋๋ฅผ ์ถ์ ํด ์ฌ์ดํด์ ํ์งํ๋ค.
๊ฐ์ ์ ์: ํ์ฌ ๊ตฌํ์ด ์ ์ ํด ๋ณด์ ๋๋ค.
๐ก ํ์ด์ ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋๋ฅผ ์ฃผ์์ผ๋ก ๋จ๊ฒจ๋ณด์ธ์!
Contributor
๐ freemjstudio ๋์ ํ์ต ํํฉ์ด๋ฒ ์ฃผ ์ ์ถ ๋ฌธ์
๋์ ํ์ต ์์ฝ
๋ฌธ์ ํ์ด ํํฉ
๐ค ์ด ๋๊ธ์ GitHub App์ ํตํด ์๋์ผ๋ก ์์ฑ๋์์ต๋๋ค. ๐ข API ์ฌ์ฉ๋ (gpt-5-nano)
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
๋ต์ ์ ์ถ ๋ฌธ์
์์ฑ์ ์ฒดํฌ ๋ฆฌ์คํธ
In Review๋ก ์ค์ ํด์ฃผ์ธ์.๊ฒํ ์ ์ฒดํฌ ๋ฆฌ์คํธ
Important
๋ณธ์ธ ๋ต์ ์ ์ถ ๋ฟ๋ง ์๋๋ผ ๋ค๋ฅธ ๋ถ PR ํ๋ ์ด์์ ๋ฐ๋์ ๊ฒํ ๋ฅผ ํด์ฃผ์ ์ผ ํฉ๋๋ค!