Skip to content

[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 7 commits into from
Jun 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions binary-tree-maximum-path-sum/printjin-gmailcom.py
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
16 changes: 16 additions & 0 deletions graph-valid-tree/printjin-gmailcom.py
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
10 changes: 10 additions & 0 deletions merge-intervals/printjin-gmailcom.py
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
6 changes: 6 additions & 0 deletions missing-number/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XOR로 풀어봐도 좋을것 같아요~

def missingNumber(self, nums):
n = len(nums)
expected_sum = n * (n + 1) // 2
actual_sum = sum(nums)
return expected_sum - actual_sum
18 changes: 18 additions & 0 deletions reorder-list/printjin-gmailcom.py
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 = []
Copy link
Contributor

Choose a reason for hiding this comment

The 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