-
-
Notifications
You must be signed in to change notification settings - Fork 335
- #2590
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
Closed
+113
−0
Closed
- #2590
Changes from all commits
Commits
Show all changes
6 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,113 @@ | ||
| """ | ||
| [결과 요약] | ||
| # 시도한 로직 수: 2 | ||
| 1. 위상 정렬로 푸는 방법 | ||
| - 시간복잡도 O(n) / 공간복잡도 O(n) | ||
| - 실제로는 numCourses + len(prerequisites) 만큼의 복잡도 (O(V+E)) | ||
| - 원리: 모든 노드를 정렬할 수 있는지 체크(순환이 있으면 모든 노드 정렬 불가) | ||
| 2. DFS를 활용하는 방법 | ||
| - 1번과 동일하게 O(V+E) | ||
| - 원리: 탐색(비순환일때만 가능)을 완료할 수 있는지 체크해서 순환 여부를 판단 | ||
|
|
||
| """ | ||
|
|
||
|
|
||
| class Solution: | ||
| def canFinish(self, numCourses: int, prerequisites: list[list[int]]) -> bool: | ||
| # 3. DFS 함수 정의 | ||
| def dfs(current: int) -> bool: | ||
| match course_states[current]: | ||
| case 2: # 탐색 완료 | ||
| return True | ||
| case 1: # 탐색 중 | ||
| return False | ||
| case 0: # 탐색 전 | ||
| course_states[current] = 1 | ||
| for next_ in course_graph[current]: | ||
| if not dfs(next_): | ||
| return False | ||
|
|
||
| course_states[current] = 2 | ||
| return True | ||
|
|
||
| # 1. 인덱스가 0부터 시작하는 그래프 초기화 | ||
| course_graph: list[list[int]] = [[] for _ in range(numCourses)] | ||
| for after, before in prerequisites: | ||
| course_graph[before].append(after) | ||
|
|
||
| # 2. 각 노드별 탐색 상태를 표시하는 객체 | ||
| course_states: list[int] = [0] * numCourses | ||
|
|
||
| # 3. 모든 노드를 1회씩 순회하면서 dfs | ||
| for course in range(numCourses): | ||
| if not dfs(course): | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
| """ | ||
| # 위상정렬 | ||
| from collections import deque | ||
|
|
||
| class Solution: | ||
| def canFinish(self, numCourses: int, prerequisites: list[list[int]]) -> bool: | ||
| # 1-a. 인덱스가 0부터 시작하는 그래프 초기화 | ||
| course_graph: list[list[int]] = [[] for _ in range(numCourses)] | ||
|
|
||
| # 1-b. 각 노드별 선행 조건 갯수 리스트 | ||
| required_courses: list[int] = [0] * numCourses | ||
|
|
||
| # 2. 선수과목을 돌면서 그래프를 채우기 | ||
| # Prerequisite이 [A, B]이면 A(After)를 수강하기 전에 B(Before)가 필요 | ||
| for after, before in prerequisites: | ||
| course_graph[before].append(after) | ||
| required_courses[after] += 1 | ||
|
|
||
| # 3. 현재 수강 가능한 강의 체크 | ||
| available_courses = deque() | ||
| for i in range(numCourses): | ||
| if required_courses[i] == 0: | ||
| available_courses.append(i) | ||
|
|
||
| # 4. queue가 빌 때까지 | ||
| done = 0 | ||
| while available_courses: | ||
|
|
||
| # 수강 가능한 강의를 하나 꺼내서 수강 처리 | ||
| current = available_courses.popleft() | ||
| done += 1 | ||
|
|
||
| # 이 강의의 선행 강의 찾기 | ||
| for next_ in course_graph[current]: | ||
| required_courses[next_] -= 1 | ||
|
|
||
| if required_courses[next_] == 0: | ||
| available_courses.append(next_) | ||
|
|
||
| return done == numCourses | ||
| """ | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
|
|
||
| test_cases = [ | ||
| (2, [[1, 0]], True), | ||
| (2, [[1, 0], [0, 1]], False), | ||
| (3, [], True), | ||
| (4, [[1, 0], [2, 1], [3, 2]], True), | ||
| (5, [[1, 0], [2, 0], [3, 1], [3, 2], [4, 3]], True), | ||
| (1, [[0, 0]], False), | ||
| (4, [[1, 0], [2, 1], [3, 2], [1, 3]], False), | ||
| ] | ||
|
|
||
| solution = Solution() | ||
|
|
||
| for idx, (numCourses, prerequisites, expected) in enumerate(test_cases, start=1): | ||
|
|
||
| result = solution.canFinish(numCourses, prerequisites) | ||
|
|
||
| assert ( | ||
| result == expected | ||
| ), f"Test Case {idx} Failed: Expected {expected}, Got {result}" | ||
|
|
||
| print("All test cases passed.") |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
Solution.canFinish (DFS)— Time: O(V + E) / Space: O(V + E)피드백: 그래프의 노드 수와 간선 수에 비례하는 DFS 탐색을 수행하며, 탐색 상태를 기록하여 순환 여부를 판단한다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
Solution.canFinish (위상 정렬)— Time: O(V + E) / Space: O(V + E)피드백: 선수 과목 수를 세고, 가능한 강의를 차례로 처리하며, 모든 강의를 수강할 수 있는지 여부를 판단한다.
개선 제안: 현재 구현이 적절해 보입니다.