Skip to content

Commit e33df7b

Browse files
authored
Merge pull request #1819 from jongwanra/main
[jongwanra] WEEK 04 Solutions
2 parents 23cbc9b + c215e49 commit e33df7b

File tree

3 files changed

+128
-1
lines changed

3 files changed

+128
-1
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
[Problem]
3+
https://leetcode.com/problems/maximum-depth-of-binary-tree/description/
4+
5+
Return maximum depth of binary tree
6+
0 < the number of nodes < 10,000
7+
8+
[Brainstorming]
9+
DFSλ₯Ό μ΄μš©ν•΄μ„œ κ΅¬ν•΄λ³΄μž.
10+
이진 νŠΈλ¦¬μ—μ„œ 전체 탐색을 DFS둜 μ§„ν–‰ν–ˆμ„ λ–„ μ‹œκ°„ λ³΅μž‘λ„λŠ” O(N)으둜 νŒλ‹¨λœλ‹€.
11+
12+
[Complexity]
13+
N is the number of nodes
14+
Time: O(N)
15+
Space: O(N)
16+
-> node의 개수 만큼 call stack이 μŒ“μž„
17+
"""
18+
19+
from typing import Optional
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+
class Solution:
27+
def maxDepth(self, root: Optional[TreeNode]) -> int:
28+
answer = 0
29+
30+
def dfs(depth, node:Optional[TreeNode])->None:
31+
nonlocal answer
32+
if not node.left and not node.right:
33+
answer = max(answer, depth)
34+
return
35+
36+
if node.left:
37+
dfs(depth + 1, node.left)
38+
if node.right:
39+
dfs(depth + 1, node.right)
40+
41+
if not root:
42+
return answer
43+
dfs(1, root)
44+
return answer
45+
46+
47+
class AnotherSolution:
48+
"""
49+
ref: leetcode
50+
51+
[Complexity]
52+
Time: O(N)
53+
Space: O(N)
54+
"""
55+
def maxDepth(self, root:Optional[TreeNode])-> int:
56+
if not root:
57+
return 0
58+
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
59+
60+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
[Problem]
3+
https://leetcode.com/problems/merge-two-sorted-lists/description/
4+
5+
두 μ •λ ¬λœ λ§ν¬λ“œ 리슀트의 Headκ°€ μ£Όμ–΄μ§„λ‹€.
6+
두 리슀트λ₯Ό ν•˜λ‚˜μ˜ μ •λ ¬λœ 리슀트둜 ν•©μΉ˜μ‹œμ˜€.
7+
merged linked list의 Headλ₯Ό λ°˜ν™˜ν•˜μž.
8+
9+
[Brainstorming]
10+
두 개의 Sorted Linked Listd의 Headκ°€ μ£Όμ–΄μ‘Œμ„ λ–„, 이미 정렬이 λ˜μ–΄ 있기 떄문에
11+
λ°˜λ³΅λ¬Έμ„ 총 2번 μˆœνšŒν•˜μ—¬ ν•˜λ‚˜μ˜ Merged Linked Listλ₯Ό λ§Œλ“€ 수 μžˆλ‹€.
12+
13+
[Complexity]
14+
N: list1.length, M: list2.length
15+
Time: O(N + M)
16+
Space: O(N + M)
17+
18+
[Todo]
19+
- μž¬κ·€μ μœΌλ‘œ 풀어보기.
20+
21+
"""
22+
from typing import Optional, List
23+
24+
25+
# Definition for singly-linked list.
26+
class ListNode:
27+
def __init__(self, val=0, next=None):
28+
self.val = val
29+
self.next = next
30+
31+
class Solution:
32+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
33+
dummy_head = ListNode(0)
34+
current_node = dummy_head
35+
while list1 and list2:
36+
if list1.val > list2.val:
37+
current_node.next = ListNode(list2.val)
38+
list2 = list2.next
39+
else:
40+
current_node.next = ListNode(list1.val)
41+
list1 = list1.next
42+
current_node = current_node.next
43+
44+
current_node.next = list1 or list2
45+
46+
return dummy_head.next
47+
48+
49+
def generate(list:List[int])->Optional[ListNode]:
50+
dummy_head = ListNode(0)
51+
current_node = dummy_head
52+
for x in list:
53+
current_node.next = ListNode(x)
54+
current_node = current_node.next
55+
return dummy_head.next
56+
57+
def print_list(node:Optional[ListNode])->None:
58+
list = []
59+
while node:
60+
list.append(node.val)
61+
node = node.next
62+
print(list)
63+
64+
sol = Solution()
65+
print_list(sol.mergeTwoLists(generate([1,2,4]), generate([1,3,4])))
66+
67+

β€Žvalid-palindrome/jongwanra.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[Problem]
33
https://leetcode.com/problems/valid-palindrome/description/
44
5-
λͺ¨λ“  λŒ€λ¬Έμžλ₯Ό μ†Œλ¬Έμžλ‘œ λ³€ν™˜ν•˜κ³  μ•ŒνŒŒλ²³κ³Ό μˆ«μžκ°€ μ•„λ‹Œ λ¬Έμžλ“€μ„ μ „λΆ€ μ œκ±°ν•˜ν•œ 이후에 μ•žμ—μ„œ λΆ€ν„° μΌμœΌλ‚˜ λ’€μ—μ„œ λΆ€ν„° μ½λ‚˜ λ™μΌν•˜κ²Œ μ½νžŒλ‹€λ©΄, κ·Έ λ¬Έμž₯은 νšŒλ¬Έμž…λ‹ˆλ‹€.
5+
λͺ¨λ“  λŒ€λ¬Έμžλ₯Ό μ†Œλ¬Έμžλ‘œ λ³€ν™˜ν•˜κ³  μ•ŒνŒŒλ²³κ³Ό μˆ«μžκ°€ μ•„λ‹Œ λ¬Έμžλ“€μ„ μ „λΆ€ μ œκ±°ν•œ 이후에 μ•žμ—μ„œ λΆ€ν„° μ½μœΌλ‚˜ λ’€μ—μ„œ λΆ€ν„° μ½μœΌλ‚˜ λ™μΌν•˜κ²Œ μ½νžŒλ‹€λ©΄, κ·Έ λ¬Έμž₯은 νšŒλ¬Έμž…λ‹ˆλ‹€.
66
영숫자 λ¬Έμžλ“€μ€ μ•ŒνŒŒλ²³κ³Ό μˆ«μžλ“€μ„ ν¬ν•¨ν•©λ‹ˆλ‹€.
77
88
[Brainstorming]

0 commit comments

Comments
Β (0)