Skip to content

Commit c67b9ac

Browse files
authored
[daehyun99] WEEK 04 Solutions (#2750)
* Solve: mergeTwoLists * Solve: maxDepth * Solve: findMin * Solve: exist
1 parent 5baef7a commit c67b9ac

4 files changed

Lines changed: 119 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def findMin(self, nums: List[int]) -> int:
3+
if len(nums) == 1:
4+
return nums[0]
5+
6+
while True:
7+
if nums[-1] < nums[0]:
8+
nums[0] = nums.pop()
9+
else:
10+
break
11+
12+
return nums[0]
13+
14+
"""
15+
class Solution:
16+
def findMin(self, nums: List[int]) -> int:
17+
return min(nums)
18+
"""
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def maxDepth(self, root: Optional[TreeNode]) -> int:
9+
count = 0
10+
stacks1 = [root]
11+
stacks2= []
12+
13+
if root is None:
14+
return count
15+
while len(stacks1) > 0 :
16+
count += 1
17+
while len(stacks1) > 0 :
18+
node = stacks1.pop()
19+
20+
if node.left is not None:
21+
stacks2.append(node.left)
22+
if node.right is not None:
23+
stacks2.append(node.right)
24+
stacks1.extend(stacks2)
25+
stacks2 = []
26+
return count
27+
28+
"""
29+
class Solution:
30+
def maxDepth(self, root: Optional[TreeNode]) -> int:
31+
def depth(node, dep):
32+
if (node.left is None) and (node.right is None):
33+
return dep
34+
if (node.left is not None) and (node.right is None):
35+
return depth(node.left, dep+1)
36+
if (node.left is None) and (node.right is not None):
37+
return depth(node.right, dep+1)
38+
else:
39+
return max(depth(node.left, dep+1), depth(node.right, dep+1))
40+
if root is None:
41+
return 0
42+
return depth(root, 1)
43+
"""
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
8+
# Space: O(N)
9+
# Time: O(N)
10+
dummy = ListNode(val=0, next=None)
11+
pointer = dummy
12+
while (list1 is not None) and (list2 is not None):
13+
if list1.val <= list2.val:
14+
pointer.next = ListNode(list1.val)
15+
list1 = list1.next
16+
else:
17+
pointer.next = ListNode(list2.val)
18+
list2 = list2.next
19+
pointer = pointer.next
20+
if list1 is not None:
21+
pointer.next = list1
22+
elif list2 is not None:
23+
pointer.next = list2
24+
25+
return dummy.next

word-search/daehyun99.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from collections import defaultdict
2+
class Solution:
3+
def exist(self, board: List[List[str]], word: str) -> bool:
4+
pos = defaultdict(list)
5+
for i in range(len(board)):
6+
for j in range(len(board[0])):
7+
if board[i][j] in word:
8+
tmp = pos.get(board[i][j], [])
9+
tmp.append([i, j])
10+
pos[board[i][j]] = tmp
11+
12+
paths = pos.get(word[0], [])
13+
paths = [[path] for path in paths]
14+
15+
for w in word[1:]:
16+
temp = []
17+
18+
while len(paths) > 0:
19+
path = paths.pop()
20+
i , j = path[-1]
21+
if ([i, j+1] in pos[w]) and ([i, j+1] not in path):
22+
temp.append(path[:] + [[i, j+1]])
23+
if ([i, j-1] in pos[w]) and ([i, j-1] not in path):
24+
temp.append(path[:] + [[i, j-1]])
25+
if ([i+1, j] in pos[w]) and ([i+1, j] not in path):
26+
temp.append(path[:] + [[i+1, j]])
27+
if ([i-1, j] in pos[w]) and ([i-1, j] not in path):
28+
temp.append(path[:] + [[i-1, j]])
29+
paths.extend(temp)
30+
for path in paths:
31+
if len(path) == len(word):
32+
return True
33+
return False

0 commit comments

Comments
 (0)