-
-
Notifications
You must be signed in to change notification settings - Fork 195
[KwonNayeon] Week 12 #1050
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
[KwonNayeon] Week 12 #1050
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a9ced98
chore: add placeholder for "Same Tree" problem
KwonNayeon 9e611b5
solve: Same Tree
KwonNayeon 3f7d0f8
solve: Remove Nth Node From End of List
KwonNayeon 2c5eb23
solve: Non-overlapping Intervals
KwonNayeon a0a82eb
Edit file name to fix error
KwonNayeon 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,36 @@ | ||
""" | ||
Constraints: | ||
- 1 <= intervals.length <= 10^5 | ||
- intervals[i].length == 2 | ||
- -5 * 10^4 <= starti < endi <= 5 * 10^4 | ||
|
||
Time Complexity: O(n * log n) | ||
- 구간 정렬 시 O(n * log n) 사용, 정렬된 구간을 순회할 때 O(n) | ||
|
||
Space Complexity: O(1) | ||
- 정해진 변수 외에는 공간 사용하지 않음 | ||
|
||
풀이방법: | ||
1. 끝점을 기준으로 오름차순 정렬 <- 제거할 구간의 수를 최소화하기 위해 | ||
2. 첫 번째 구간을 선택하고 그 구간의 끝점을 기록함 | ||
3. 두 번째 구간부터 end보다 시작점이 크거나 같은(겹치지 않는) 구간을 찾아서 카운트 | ||
4. 전체 구간의 수에서 count를 빼서 제거해야 할 구간의 수를 반환 | ||
""" | ||
class Solution: | ||
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: | ||
if not intervals: | ||
return 0 | ||
|
||
intervals.sort(key=lambda x: x[1]) | ||
|
||
count = 1 | ||
end = intervals[0][1] | ||
|
||
for interval in intervals[1:]: | ||
|
||
if interval[0] >= end: | ||
count += 1 | ||
end = interval[1] | ||
|
||
return len(intervals) - 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 @@ | ||
""" | ||
Constraints: | ||
- The number of nodes in the list is sz. | ||
- 1 <= sz <= 30 | ||
- 0 <= Node.val <= 100 | ||
- 1 <= n <= sz | ||
|
||
Time Complexity: O(n) | ||
- 여기서 n은 리스트의 길이, 리스트를 한 번만 순회함 | ||
|
||
Space Complexity: O(1) | ||
- 추가 공간을 사용하지 않음, 정해진 개수의 변수만 사용 | ||
|
||
풀이방법: | ||
1. fast 포인터를 n번 앞으로 이동 | ||
2. base case: 만약 fast가 None이라면, 첫 번째 노드를 제거함 | ||
3. fast.next가 None일 때까지 두 포인터를 함께 이동 | ||
4. slow의 다음 노드를 제거함 (slow.next = slow.next.next로 연결을 끊어냄) | ||
""" | ||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, val=0, next=None): | ||
# self.val = val | ||
# self.next = next | ||
class Solution: | ||
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: | ||
slow = head | ||
fast = head | ||
|
||
for i in range(n): | ||
fast = fast.next | ||
|
||
if not fast: | ||
return head.next | ||
|
||
while fast.next: | ||
slow = slow.next | ||
fast = fast.next | ||
|
||
slow.next = slow.next.next | ||
|
||
return head |
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,34 @@ | ||
""" | ||
Constraints: | ||
- The number of nodes in both trees is in the range [0, 100]. | ||
- -10^4 <= Node.val <= 10^4 | ||
|
||
Time Complexity: O(n) | ||
- 각 노드를 한 번씩 방문 | ||
|
||
Space Complexity: O(h) | ||
- 재귀 호출 스택의 깊이는 트리의 높이(h)에 비례함 | ||
|
||
풀이방법: | ||
1. DFS와 재귀를 활용하여 두 트리를 동시에 탐색 | ||
2. base case: | ||
- p와 q가 모두 None이면 → 같은 트리 | ||
- 둘 중 하나만 None이거나 노드의 값이 다르면 → 다른 트리 | ||
3. 재귀로 왼쪽과 오른쪽 서브트리가 모두 같은지 확인 | ||
""" | ||
# 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 is None and q is None: | ||
return True | ||
|
||
if p is None or q is None or 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.
LinkedList 의 특성을 잘 활용하셔서 풀이해 주신것 같아요 :)