-
-
Notifications
You must be signed in to change notification settings - Fork 194
[printjin-gmailcom] week 12 solutions #1584
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1bab0c4
Solve : Same Tree
printjin-gmailcom 44381e9
Solve : Remove Nth Node From End of List
printjin-gmailcom aae3ae1
Solve : Number of Connected Components in an Undirected Graph
printjin-gmailcom 28a5579
Solve : Non Overlapping Intervals
printjin-gmailcom 13f26f2
Solve : Serialize And Deserialize Binary Tree
printjin-gmailcom 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,13 @@ | ||
from typing import List | ||
|
||
class Solution: | ||
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int: | ||
intervals.sort(key=lambda x: x[1]) | ||
count = 0 | ||
end = float('-inf') | ||
for start, finish in intervals: | ||
if start < end: | ||
count += 1 | ||
else: | ||
end = finish | ||
return count |
18 changes: 18 additions & 0 deletions
18
number-of-connected-components-in-an-undirected-graph/printjin-gmailcom.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,18 @@ | ||
from typing import ( | ||
List, | ||
) | ||
|
||
class Solution: | ||
def count_components(self, n: int, edges: List[List[int]]) -> int: | ||
parent = [i for i in range(n)] | ||
def find(x): | ||
if parent[x] != x: | ||
parent[x] = find(parent[x]) | ||
return parent[x] | ||
for a, b in edges: | ||
root_a = find(a) | ||
root_b = find(b) | ||
if root_a != root_b: | ||
parent[root_a] = root_b | ||
n -= 1 | ||
return n |
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,11 @@ | ||
class Solution: | ||
def removeNthFromEnd(self, head, n): | ||
dummy = ListNode(0, head) | ||
fast = slow = dummy | ||
for _ in range(n): | ||
fast = fast.next | ||
while fast.next: | ||
fast = fast.next | ||
slow = slow.next | ||
slow.next = slow.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,9 @@ | ||
class Solution: | ||
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: | ||
if not p and not q: | ||
return True | ||
if not p or not q: | ||
return False | ||
if p.val != q.val: | ||
return False | ||
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) |
26 changes: 26 additions & 0 deletions
26
serialize-and-deserialize-binary-tree/printjin-gmailcom.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,26 @@ | ||
class Codec: | ||
def serialize(self, root): | ||
def dfs(node): | ||
if not node: | ||
vals.append("None") | ||
return | ||
vals.append(str(node.val)) | ||
dfs(node.left) | ||
dfs(node.right) | ||
|
||
vals = [] | ||
dfs(root) | ||
return ",".join(vals) | ||
|
||
def deserialize(self, data): | ||
def dfs(): | ||
val = next(vals) | ||
if val == "None": | ||
return None | ||
node = TreeNode(int(val)) | ||
node.left = dfs() | ||
node.right = dfs() | ||
return node | ||
|
||
vals = iter(data.split(",")) | ||
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.
의 형태로 조건문을 더욱 간략하게 줄일수도 있을것 같습니다!
(워낙 간결한 코드라, 이정도 코멘트밖에 드릴게 없네요.. 🤣)