Skip to content

Commit

Permalink
[Leo] 2nd Week solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
leokim0922 committed May 7, 2024
1 parent bfd7843 commit 544837d
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
17 changes: 17 additions & 0 deletions invert-binary-tree/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 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 invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return None

else:
root.right, root.left = self.invertTree(root.left), self.invertTree(root.right)

return root

## TC: O(n), SC: O(n), avg O(logn) if the given tree is balanced
31 changes: 31 additions & 0 deletions linked-list-cycle/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
slow = head
fast = head

while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True

return False

# visited = head

# while visited:
# if visited.val == None:
# return True

# visited.val = None
# visited = visited.next

# return False

# Both TC:O(n) and SC:O(1), but below one is kinda tricky solution
20 changes: 20 additions & 0 deletions merge-two-sorted-lists/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
if not list1 or not list2:
return list1 or list2

if list1.val <= list2.val:
list1.next = self.mergeTwoLists(list1.next, list2)
return list1

else:
list2.next = self.mergeTwoLists(list1, list2.next)
return list2

## TC: O(n) or O(n+m), depends on the length of list1 and list2
## SC: O(max(m,n))
18 changes: 18 additions & 0 deletions reverse-linked-list/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev, curr = None, head

while curr:
tmp = curr.next ## save next node
curr.next = prev ## reverse next pointer to the prev
prev = curr ## update prev pointer with curr, since it's reversed now
curr = tmp ## move on to the next saved node

return prev

## TC: O(n) SC: O(1)
16 changes: 16 additions & 0 deletions valid-parentheses/Leo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def isValid(self, s: str) -> bool:

d = {'(': ')', '[': ']', '{': '}'}
stack = []

for i in s:
if i in d:
stack.append(i)
elif len(stack) == 0 or d[stack.pop()] != i:
return False

return len(stack) == 0

## O(n) time complexity but not that much fast by leetcode system...
## Space complexity is also O(n)

0 comments on commit 544837d

Please sign in to comment.