Skip to content

Commit a01dfd2

Browse files
authored
Merge pull request #57 from bhyun-kim/main
[김병현, bhyun-kim] Solution for Week2 Assignment
2 parents a3fc68a + 6c2cb7a commit a01dfd2

File tree

5 files changed

+207
-0
lines changed

5 files changed

+207
-0
lines changed

invert-binary-tree/bhyun-kim.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
226. Invert Binary Tree
3+
https://leetcode.com/problems/invert-binary-tree/description/
4+
5+
Solution
6+
Recursively swaps the left and right children of the root node.
7+
8+
1. Check if the root has left and right children.
9+
2. If it does, invert the left and right children.
10+
3. If it doesn't, invert the left or right child.
11+
4. Return the root.
12+
13+
Time complexity: O(n)
14+
Space complexity: O(n)
15+
16+
"""
17+
from typing import Optional
18+
19+
20+
# Definition for a binary tree node.
21+
class TreeNode:
22+
def __init__(self, val=0, left=None, right=None):
23+
self.val = val
24+
self.left = left
25+
self.right = right
26+
27+
28+
class Solution:
29+
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
30+
has_left = hasattr(root, "left")
31+
has_right = hasattr(root, "right")
32+
33+
if has_left and has_right:
34+
root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
35+
elif has_left:
36+
root.left, root.right = None, self.invertTree(root.left)
37+
elif has_right:
38+
root.left, root.right = self.invertTree(root.right), None
39+
40+
return root

linked-list-cycle/bhyun-kim.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
141. Linked List Cycle
3+
https://leetcode.com/problems/linked-list-cycle/description/
4+
5+
Solution
6+
Floyd's Tortoise and Hare algorithm:
7+
8+
1. Initialize two pointers, slower and faster, to the head of the linked list.
9+
faster is one step ahead of slower.
10+
2. Move the slower pointer by one and the faster pointer by two.
11+
3. If the slower and faster pointers meet, return True.
12+
4. If the faster pointer reaches the end of the linked list, return False.
13+
14+
Time complexity: O(n)
15+
Space complexity: O(1)
16+
"""
17+
18+
19+
from typing import Optional
20+
21+
22+
# Definition for singly-linked list.
23+
class ListNode:
24+
def __init__(self, x):
25+
self.val = x
26+
self.next = None
27+
28+
29+
class Solution:
30+
def hasCycle(self, head: Optional[ListNode]) -> bool:
31+
if not hasattr(head, "next"):
32+
return False
33+
34+
slower = head
35+
faster = head.next
36+
37+
while slower != faster:
38+
if not hasattr(faster, "next"):
39+
return False
40+
elif not hasattr(faster.next, "next"):
41+
return False
42+
slower = slower.next
43+
faster = faster.next.next
44+
45+
return True

merge-two-sorted-lists/bhyun-kim.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
21. Merge Two Sorted Lists
3+
https://leetcode.com/problems/merge-two-sorted-lists/
4+
5+
Solution
6+
Recursively merges two sorted linked lists.
7+
8+
1. Check if either list is empty.
9+
2. Compare the values of the two lists.
10+
3. Return a new node with the smaller value and the merged list.
11+
12+
Time complexity: O(n)
13+
Space complexity: O(n)
14+
"""
15+
from typing import Optional
16+
17+
18+
# Definition for singly-linked list.
19+
class ListNode:
20+
def __init__(self, val=0, next=None):
21+
self.val = val
22+
self.next = next
23+
24+
25+
class Solution:
26+
def mergeTwoLists(
27+
self, list1: Optional[ListNode], list2: Optional[ListNode]
28+
) -> Optional[ListNode]:
29+
if list1 is None:
30+
return list2
31+
32+
if list2 is None:
33+
return list1
34+
35+
if list1.val < list2.val:
36+
val = list1.val
37+
list1 = list1.next
38+
else:
39+
val = list2.val
40+
list2 = list2.next
41+
42+
return ListNode(val=val, next=self.mergeTwoLists(list1, list2))

reverse-linked-list/bhyun-kim.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
206. Reverse Linked List
3+
https://leetcode.com/problems/reverse-linked-list/description/
4+
5+
Solution
6+
Iteratively reverses a singly linked list.
7+
8+
1. Initialize two pointers, prev and curr, to None and the head of the linked list, respectively.
9+
2. While curr is not None:
10+
a. Save the next node of curr.
11+
b. Set the next node of curr to prev.
12+
c. Move prev and curr to the next nodes.
13+
14+
Time complexity: O(n)
15+
Space complexity: O(1)
16+
17+
"""
18+
19+
20+
from typing import Optional
21+
22+
23+
# Definition for singly-linked list.
24+
class ListNode:
25+
def __init__(self, val=0, next=None):
26+
self.val = val
27+
self.next = next
28+
29+
30+
class Solution:
31+
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
32+
prev, curr = None, head
33+
34+
while curr:
35+
next_temp = curr.next
36+
curr.next = prev
37+
prev = curr
38+
curr = next_temp
39+
40+
return prev

valid-parentheses/bhyun-kim.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
20. Valid Parentheses
3+
https://leetcode.com/problems/valid-parentheses/description/
4+
5+
Solution
6+
Uses a stack to check if the parentheses are valid.
7+
8+
1. Initialize an empty stack.
9+
2. Iterate through each character in the string.
10+
3. If the character is an opening parenthesis,
11+
push it to the stack.
12+
4. If the character is a closing parenthesis,
13+
pop the stack and check if the opening parenthesis matches.
14+
5. If the stack is empty, return True.
15+
6. Otherwise, return False.
16+
17+
Time complexity: O(n)
18+
Space complexity: O(n)
19+
"""
20+
21+
22+
class Solution:
23+
def isValid(self, s: str) -> bool:
24+
stack = ""
25+
opening = ["(", "[", "{"]
26+
closing = [")", "]", "}"]
27+
28+
for _s in s:
29+
if _s in opening:
30+
stack = _s + stack
31+
else:
32+
if stack:
33+
if opening[closing.index(_s)] == stack[0]:
34+
stack = stack[1:]
35+
else:
36+
return False
37+
else:
38+
return False
39+
40+
return len(stack) == 0

0 commit comments

Comments
 (0)