-
-
Notifications
You must be signed in to change notification settings - Fork 235
[Chaedie] Week 12 #1055
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
[Chaedie] Week 12 #1055
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9fe56ce
feat: [Week 12]
Chaedie 465ba52
Merge branch 'DaleStudy:main' into main
Chaedie 392fbfa
feat: add another solution of remove-nth-node-from-end-or-list
Chaedie 2141633
Merge branch 'main' of https://github.com/Chaedie/leetcode-study
Chaedie 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,21 @@ | ||
""" | ||
Solution: 직접 풀지 못해 해설을 통해 이해하고 풀었습니다. | ||
Interval 문제가 계속 어렵네요.. 😂 | ||
Time: O(n) | ||
Space: O(1) | ||
""" | ||
|
||
|
||
class Solution: | ||
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: | ||
intervals.sort() | ||
|
||
res = 0 | ||
prevEnd = intervals[0][1] | ||
for start, end in intervals[1:]: | ||
if start >= prevEnd: | ||
prevEnd = end | ||
else: | ||
res += 1 | ||
prevEnd = min(end, prevEnd) | ||
return res |
38 changes: 38 additions & 0 deletions
38
number-of-connected-components-in-an-undirected-graph/Chaedie.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,38 @@ | ||
""" | ||
Solution: | ||
인접 그래프를 그려준다. | ||
dfs 해준다. | ||
중복제거를 위해 visit set 을 사용해야한다. | ||
모든 노드에 대해 dfs 를 하되 갯수를 count 한다. | ||
Time: O(n) | ||
Space: O(n) | ||
""" | ||
|
||
|
||
class Solution: | ||
def countComponents(self, n: int, edges: List[List[int]]) -> int: | ||
|
||
# 인접 리스트 | ||
graph = {i: [] for i in range(n)} | ||
for a, b in edges: | ||
graph[a].append(b) | ||
graph[b].append(a) | ||
|
||
visit = set() | ||
|
||
def dfs(node): | ||
if node in visit: | ||
return | ||
|
||
visit.add(node) | ||
for i in graph[node]: | ||
if i not in visit: | ||
dfs(i) | ||
|
||
count = 0 | ||
for i in range(n): | ||
if i not in visit: | ||
count += 1 | ||
dfs(i) | ||
|
||
return count |
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. 재귀를 통해 풀이하는건 처음봤네요..! 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. 새로운 풀이도 올렸습니다. |
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,55 @@ | ||
""" | ||
Solution: | ||
재귀를 통해 끝까지 간뒤 n번째 pop 될때 노드를 삭제해준다. | ||
Time: O(n) | ||
Space: O(n) | ||
""" | ||
|
||
|
||
class Solution: | ||
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: | ||
dummy = ListNode() | ||
dummy.next = head | ||
|
||
count = 0 | ||
|
||
def dfs(node, prev): | ||
nonlocal count | ||
if not node: | ||
return | ||
|
||
dfs(node.next, node) | ||
count += 1 | ||
if count == n: | ||
prev.next = node.next | ||
|
||
dfs(head, dummy) | ||
return dummy.next | ||
|
||
|
||
""" | ||
Solution: | ||
1) 2 Pointer 로 size - n 까지 이동 | ||
2) prev 와 curr.next 를 연결 | ||
Time: O(n) | ||
Space: O(1) | ||
""" | ||
class Solution: | ||
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: | ||
|
||
dummy = ListNode(0, head) | ||
prev = dummy | ||
curr = tail = head | ||
|
||
while n > 0: | ||
tail = tail.next | ||
n -= 1 | ||
|
||
while tail: | ||
prev = prev.next | ||
curr = curr.next | ||
tail = tail.next | ||
|
||
prev.next = curr.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,17 @@ | ||
""" | ||
Solution: val 이 같고, left, right 이 같으면 된다. | ||
Time: O(n) | ||
Space: O(n) | ||
""" | ||
|
||
|
||
class Solution: | ||
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: | ||
|
||
if q and p and q.val != p.val: | ||
return False | ||
if not p and not q: | ||
return True | ||
if (not p and q) or (p and not q): | ||
return False | ||
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) |
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,47 @@ | ||
class Codec: | ||
""" | ||
Solution: DFS | ||
Time: O(n) | ||
Space: O(n) | ||
""" | ||
|
||
def serialize(self, root): | ||
arr = [] | ||
|
||
def dfs(node): | ||
if not node: | ||
arr.append("null") | ||
return | ||
|
||
arr.append(str(node.val)) | ||
dfs(node.left) | ||
dfs(node.right) | ||
|
||
dfs(root) | ||
|
||
return ",".join(arr) | ||
|
||
""" | ||
Solution: DFS | ||
Time: O(n) | ||
Space: O(n) | ||
""" | ||
|
||
def deserialize(self, data): | ||
vals = data.split(",") | ||
self.i = 0 | ||
|
||
def dfs(): | ||
if vals[self.i] == "null": | ||
self.i += 1 | ||
return None | ||
|
||
node = TreeNode() | ||
node.val = vals[self.i] | ||
self.i += 1 | ||
node.left = dfs() | ||
node.right = dfs() | ||
|
||
return node | ||
|
||
return dfs() |
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.
저도 인터벌 문제를 잘 풀지는 못하지만 계속 이해하며 풀다보시면 비슷한 유형들이 눈에 익게 될거라고 믿어요!
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.
감사합니다. 어렵지만 꾸준히 해야죠 감사합니다..!!