Skip to content

[jongwanra] WEEK 04 Solutions #1819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions maximum-depth-of-binary-tree/jongwanra.py
Original file line number Diff line number Diff line change
@@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분은 문제 없지만, root가 없는 경우는 이미 위에서 처리했기 때문에 if not node: 로 처리해도 더 범용적

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))


67 changes: 67 additions & 0 deletions merge-two-sorted-lists/jongwanra.py
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기존 노드를 재사용하지 않고 새 노드를 계속 생성 → 메모리 낭비

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 = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

list라는 변수명이 Python 내장 타입과 충돌

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])))


2 changes: 1 addition & 1 deletion valid-palindrome/jongwanra.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[Problem]
https://leetcode.com/problems/valid-palindrome/description/

모든 대문자를 소문자로 변환하고 알파벳과 숫자가 아닌 문자들을 전부 제거하한 이후에 앞에서 부터 일으나 뒤에서 부터 읽나 동일하게 읽힌다면, 그 문장은 회문입니다.
모든 대문자를 소문자로 변환하고 알파벳과 숫자가 아닌 문자들을 전부 제거한 이후에 앞에서 부터 읽으나 뒤에서 부터 읽으나 동일하게 읽힌다면, 그 문장은 회문입니다.
영숫자 문자들은 알파벳과 숫자들을 포함합니다.

[Brainstorming]
Expand Down