-
-
Notifications
You must be signed in to change notification settings - Fork 194
[printjin-gmailcom] week 11 solutions #1561
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
7 commits
Select commit
Hold shift + click to select a range
37cada7
Solve : Missing Number
printjin-gmailcom 16dddfa
Merge branch 'main' of https://github.com/printjin-gmailcom/Dalle_Cod…
printjin-gmailcom faeaa2b
Solve : Reorder List
printjin-gmailcom 60f506b
Solve : Graph Valid Tree
printjin-gmailcom 6f38c76
Solve : Merge Intervals
printjin-gmailcom c34b649
Solve : Binary Tree Maximum Path Sum
printjin-gmailcom b93eb38
Error : 파일명 에러
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,12 @@ | ||
class Solution: | ||
def maxPathSum(self, root): | ||
self.max_sum = float('-inf') | ||
def dfs(node): | ||
if not node: | ||
return 0 | ||
left = max(dfs(node.left), 0) | ||
right = max(dfs(node.right), 0) | ||
self.max_sum = max(self.max_sum, node.val + left + right) | ||
return node.val + max(left, right) | ||
dfs(root) | ||
return self.max_sum |
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,16 @@ | ||
class Solution: | ||
def valid_tree(self, n, edges): | ||
if len(edges) != n - 1: | ||
return False | ||
parent = [i for i in range(n)] | ||
def find(x): | ||
while parent[x] != x: | ||
parent[x] = parent[parent[x]] | ||
x = parent[x] | ||
return x | ||
for u, v in edges: | ||
pu, pv = find(u), find(v) | ||
if pu == pv: | ||
return False | ||
parent[pu] = pv | ||
return True |
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,10 @@ | ||
class Solution: | ||
def merge(self, intervals): | ||
intervals.sort(key=lambda x: x[0]) | ||
merged = [] | ||
for interval in intervals: | ||
if not merged or merged[-1][1] < interval[0]: | ||
merged.append(interval) | ||
else: | ||
merged[-1][1] = max(merged[-1][1], interval[1]) | ||
return merged |
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,6 @@ | ||
class Solution: | ||
def missingNumber(self, nums): | ||
n = len(nums) | ||
expected_sum = n * (n + 1) // 2 | ||
actual_sum = sum(nums) | ||
return expected_sum - actual_sum |
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 @@ | ||
class Solution: | ||
def reorderList(self, head): | ||
if not head: | ||
return | ||
nodes = [] | ||
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. 제가 파이썬을 잘 모르긴한데, 문제에 in-place로 풀라고 되어있어서 이렇게 풀어도 되는건지 궁금하네요~! |
||
current = head | ||
while current: | ||
nodes.append(current) | ||
current = current.next | ||
i, j = 0, len(nodes) - 1 | ||
while i < j: | ||
nodes[i].next = nodes[j] | ||
i += 1 | ||
if i == j: | ||
break | ||
nodes[j].next = nodes[i] | ||
j -= 1 | ||
nodes[i].next = None |
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.
XOR로 풀어봐도 좋을것 같아요~