Skip to content
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
47 changes: 47 additions & 0 deletions non-overlapping-intervals/doh6077.py
Copy link
Contributor

Choose a reason for hiding this comment

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

저도 처음에는 시작점을 기준으로 정렬해 접근했지만, 종료 시점을 기준으로 한 그리디 전략이 더 적절하다고 판단하여 풀이를 변경했습니다. 👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class Solution:
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:

# # first structure it as graph (dictionary)
# intervals = [[1,2],[2,3],[3,4],[1,3]]
# dict = {}
# for i in intervals:
# dict[i[0]] = []
# for a,b in intervals:
# dict[a].append(b)

# #Overlapping edge cases
# # 1. [1,4], [2,3] -> not sure how to figure out
# # 2. [1,3], [1,2] , [3,4] -> check if the value has connection
# # 3. [1,2], [1,2], [1,2] ( same values) use hashset

# count = 0
# visited = set()
# for key, value in dict.items():
# if value not in visited:
# visited.append(value)
# else:
# count += 1
# return count

# count = 0
# visited = set()
# for a, b in intervals:
# if b not in visited:
# visited.add(b)
# else:
# count += 1
# return count


# greedy Approach
intervals.sort()

res = 0
prevEnd = intervals[0][1]
for start, end in intervals[1:]:
if start >= prevEnd:
prevEnd = end
else:
res += 1
prevEnd = min(prevEnd, end)
return res
23 changes: 23 additions & 0 deletions remove-nth-node-from-end-of-list/doh6077.py
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution:
# 19. Remove Nth Node From End of List
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
dummy = ListNode(0, head)

# Calculate Length
length = 0
current = head
while current:
length += 1
current = current.next

stop_index = length - n

current = dummy
for _ in range(stop_index):
current = current.next

# Delete the node
current.next = current.next.next

# Return the start of the list (dummy.next handles if head changed)
return dummy.next
26 changes: 26 additions & 0 deletions same-tree/doh6077.py
Copy link
Contributor

Choose a reason for hiding this comment

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

재귀 풀이 좋습니다! 달레님 풀이에 스택을 활용한 방법도 있으니 같이 보셔도 좋을듯 합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# 100. Same Tree
# https://leetcode.com/problems/same-tree/description/


# 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:
#Time Complexity: O(N)- it visits every node once
#Space Complexity: O(H)- H is the height of the tree, which is the maximum depth of the recursion stack
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
# DFS approach
def dfs(node1, node2):
if node1 is None and node2 is None:
return True
if node1 is None or node2 is None:
return False
if node1.val != node2.val:
return False
return dfs(node1.left,node2.left) and dfs(node1.right,node2.right)

return dfs(p, q)