diff --git a/maximum-depth-of-binary-tree/jongwanra.py b/maximum-depth-of-binary-tree/jongwanra.py new file mode 100644 index 0000000000..10b292730c --- /dev/null +++ b/maximum-depth-of-binary-tree/jongwanra.py @@ -0,0 +1,60 @@ +""" +[Problem] +https://leetcode.com/problems/maximum-depth-of-binary-tree/description/ + +Return maximum depth of binary tree +0 < the number of nodes < 10,000 + +[Brainstorming] +DFS를 이용해서 구해보자. +이진 트리에서 전체 탐색을 DFS로 진행했을 떄 시간 복잡도는 O(N)으로 판단된다. + +[Complexity] +N is the number of nodes +Time: O(N) +Space: O(N) + -> node의 개수 만큼 call stack이 쌓임 +""" + +from typing import Optional +# 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: + answer = 0 + + def dfs(depth, node:Optional[TreeNode])->None: + nonlocal answer + if not node.left and not node.right: + answer = max(answer, depth) + return + + if node.left: + dfs(depth + 1, node.left) + if node.right: + dfs(depth + 1, node.right) + + if not root: + return answer + dfs(1, root) + return answer + + +class AnotherSolution: + """ + ref: leetcode + + [Complexity] + Time: O(N) + Space: O(N) + """ + def maxDepth(self, root:Optional[TreeNode])-> int: + if not root: + return 0 + return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right)) + + diff --git a/merge-two-sorted-lists/jongwanra.py b/merge-two-sorted-lists/jongwanra.py new file mode 100644 index 0000000000..f0f59df7f7 --- /dev/null +++ b/merge-two-sorted-lists/jongwanra.py @@ -0,0 +1,67 @@ +""" +[Problem] +https://leetcode.com/problems/merge-two-sorted-lists/description/ + +두 정렬된 링크드 리스트의 Head가 주어진다. +두 리스트를 하나의 정렬된 리스트로 합치시오. +merged linked list의 Head를 반환하자. + +[Brainstorming] +두 개의 Sorted Linked Listd의 Head가 주어졌을 떄, 이미 정렬이 되어 있기 떄문에 +반복문을 총 2번 순회하여 하나의 Merged Linked List를 만들 수 있다. + +[Complexity] +N: list1.length, M: list2.length +Time: O(N + M) +Space: O(N + M) + +[Todo] +- 재귀적으로 풀어보기. + +""" +from typing import Optional, List + + +# 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]: + dummy_head = ListNode(0) + current_node = dummy_head + while list1 and list2: + if list1.val > list2.val: + current_node.next = ListNode(list2.val) + list2 = list2.next + else: + current_node.next = ListNode(list1.val) + list1 = list1.next + current_node = current_node.next + + current_node.next = list1 or list2 + + return dummy_head.next + + +def generate(list:List[int])->Optional[ListNode]: + dummy_head = ListNode(0) + current_node = dummy_head + for x in list: + current_node.next = ListNode(x) + current_node = current_node.next + return dummy_head.next + +def print_list(node:Optional[ListNode])->None: + list = [] + while node: + list.append(node.val) + node = node.next + print(list) + +sol = Solution() +print_list(sol.mergeTwoLists(generate([1,2,4]), generate([1,3,4]))) + + diff --git a/valid-palindrome/jongwanra.py b/valid-palindrome/jongwanra.py index db9d1ac88f..0a57dc33cd 100644 --- a/valid-palindrome/jongwanra.py +++ b/valid-palindrome/jongwanra.py @@ -2,7 +2,7 @@ [Problem] https://leetcode.com/problems/valid-palindrome/description/ -모든 대문자를 소문자로 변환하고 알파벳과 숫자가 아닌 문자들을 전부 제거하한 이후에 앞에서 부터 일으나 뒤에서 부터 읽나 동일하게 읽힌다면, 그 문장은 회문입니다. +모든 대문자를 소문자로 변환하고 알파벳과 숫자가 아닌 문자들을 전부 제거한 이후에 앞에서 부터 읽으나 뒤에서 부터 읽으나 동일하게 읽힌다면, 그 문장은 회문입니다. 영숫자 문자들은 알파벳과 숫자들을 포함합니다. [Brainstorming]