-
-
Notifications
You must be signed in to change notification settings - Fork 305
[Seoya0512] WEEK12 Solutions #2307
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
+126
−0
Merged
Changes from all commits
Commits
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,23 @@ | ||
| ''' | ||
| Time Complexity: O(n log n) | ||
| - intervals 배열을 정렬하는데 소요되는 시간 (n은 intervals의 길이) | ||
| - 배열을 순회하며 겹치는 구간을 찾는데 소요되는 시간 O(n)이나 전체 시간 복잡도에 영향을 주지 않음 | ||
|
|
||
| # Space Complexity: O(1) | ||
| - last_end, count 변수를 사용하는데 필요한 공간 | ||
| ''' | ||
|
|
||
|
|
||
| class Solution: | ||
| def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: | ||
| intervals.sort(key=lambda x: x[1]) | ||
| last_end = intervals[0][1] | ||
| count = 0 | ||
|
|
||
| for val in intervals[1:]: | ||
| if (val[0] < last_end): | ||
| print(val) | ||
| count += 1 | ||
| if (val[0] >= last_end): | ||
| last_end = val[1] | ||
| return count | ||
43 changes: 43 additions & 0 deletions
43
number-of-connected-components-in-an-undirected-graph/Seoya0512.py
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,43 @@ | ||
| ''' | ||
| Time Complexity: O(N + E) | ||
| - 노드의 개수 N, 간선의 개수 E | ||
| - 노드의 개수와 간선의 수를 합친만큼 재귀 함수 호출 필요 | ||
|
|
||
| Space Complexity: O(N + E) | ||
| - 인접 리스트의 크기가 노드와 간선의 수의 합에 비례 | ||
| - 집합에 최대 N개의 숫자 저장 | ||
| ''' | ||
| from typing import ( | ||
| List, | ||
| ) | ||
|
|
||
| class Solution: | ||
| """ | ||
| @param n: the number of vertices | ||
| @param edges: the edges of undirected graph | ||
| @return: the number of connected components | ||
| """ | ||
| def count_components(self, n: int, edges: List[List[int]]) -> int: | ||
| # 연결관계를 표현하기 위한 그래프 | ||
| graph = [[] for _ in range(n)] | ||
|
|
||
| for a,b in edges: | ||
| graph[a].append(b) | ||
| graph[b].append(a) | ||
|
|
||
| # 깊은탐색에서 노드 방문여부 표기 | ||
| visited = set() | ||
|
|
||
| def dfs(node): | ||
| # 재귀함수 실행시 방문 처리 | ||
| visited.add(node) | ||
| for adj in graph[node]: | ||
| if adj not in visited: | ||
| dfs(adj) | ||
|
|
||
| count = 0 | ||
| for node in range(n): | ||
| if node not in visited: | ||
| dfs(node) | ||
| count += 1 | ||
| return count |
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,42 @@ | ||
| class Solution: | ||
| def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: | ||
|
|
||
| def remove_node(node): | ||
| if node is None: | ||
| return 0 | ||
| # 2. 재귀 호출 | ||
| count = remove_node(node.next) + 1 | ||
| # 4. 삭제 로직 (count가 n+1일 때) | ||
| if count == n + 1: | ||
| node.next = node.next.next | ||
| # 5. 현재 count 반환 | ||
| return count | ||
|
|
||
| # 재귀 함수 호출 시작 | ||
| total_count = remove_node(head) | ||
|
|
||
| if total_count == n: | ||
| return head.next | ||
| return head | ||
|
|
||
|
|
||
| # Two Pointer Solution | ||
| # One loop | ||
|
|
||
| class Solution: | ||
| def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: | ||
| first = head | ||
| # 삭제할 노드의 바로 전 노드에 도착 | ||
| for _ in range(n+1): | ||
| first = first.next | ||
|
|
||
| dummy = ListNode(None, head) | ||
| second = dummy | ||
|
|
||
| while first: | ||
| first = first.next | ||
| second = second.next | ||
|
|
||
| second.next = second.next.next | ||
|
|
||
| return dummy.next |
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,18 @@ | ||
| # Definition for a binary tree node. | ||
| # class TreeNode: | ||
| # def __init__(self, val=0, left=None, right=None): | ||
| # self.val = val | ||
| # self.left = left | ||
| # self.right = right | ||
| class Solution: | ||
| def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: | ||
| if p == None and q == None: | ||
| return True | ||
|
|
||
| if p == None or q == None: | ||
| return False | ||
|
|
||
| if p.val != q.val: | ||
| return False | ||
|
|
||
| return self.isSameTree(p.left, q.left) and self.isSameTree(p.right,q.right) |
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.
저는 시작점을 기준으로 sorting해서 interval이 발생했을때 비교하는 코드가 필요했는데, 작성해주신 것처럼 end 기준으로 할 경우 그 부분이 필요 없겠네요. 배워갑니다!