Skip to content
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

[saysimple] 3주차 문제 풀이 #83

Merged
merged 3 commits into from
May 22, 2024
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
10 changes: 10 additions & 0 deletions climbing-stairs/saysimple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## TC: O(n), SC:O(1)

class Solution:
def climbStairs(self, n: int) -> int:
a, b = 1, 1

for i in range(n - 1):
a, b = a + b, a

return a
11 changes: 11 additions & 0 deletions maximum-depth-of-binary-tree/saysimple.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,11 @@
# 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:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
16 changes: 16 additions & 0 deletions meeting-rooms/saysimple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# TC: O(n), SC: O(1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Time complexity 착각하신거 같아요!

class Solution:
def canAttendMeetings(self, intervals: List[Interval]) -> bool:
if not intervals:
return True

intervals.sort(key=lambda x: x.start)

e = intervals[0].end

for i in range(1, len(intervals)):
if intervals[i].start < e:
return False
e = intervals[i].end

return True
27 changes: 27 additions & 0 deletions same-tree/saysimple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# TC: O(n), SC:O(n)
# 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:
t = []
def pre_order(self, node: TreeNode):
if node is None:
self.t.append(None)
return

self.t.append(node.val)
self.pre_order(node.left)
self.pre_order(node.right)

def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
self.t = []
self.pre_order(p)
a = self.t[:]
self.t = []
self.pre_order(q)
b = self.t[:]

return a == b
23 changes: 23 additions & 0 deletions subtree-of-another-tree/saysimple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# TC: O(mn), SC: O(m+n)
# 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:
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
def prev(root, subRoot):
if not root or not subRoot:
return root == subRoot
if root.val != subRoot.val:
return False

return prev(root.left, subRoot.left) and prev(root.right, subRoot.right)

if not root:
return False
if prev(root, subRoot):
return True

return self.isSubtree(root.left, subRoot) or self.isSubtree(root.right, subRoot)