Skip to content
Open
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
31 changes: 31 additions & 0 deletions maximum-depth-of-binary-tree/hyedall.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
# https://leetcode.com/problems/maximum-depth-of-binary-tree/
시간 복잡도 : O(n)
공간 복잡도 : O(n)
"""

# 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

스택을 이용한 DF 풀이군요. 흥미롭게 잘봤습니다.

class Solution:
def maxDepth(self, root: TreeNode | None) -> int:
if not root:
return 0

answer = 0
stack = [(root, 1)]

while stack:
cur, depth = stack.pop()
answer = max(answer, depth)

if cur.left:
stack.append((cur.left, depth+1))
if cur.right:
stack.append((cur.right, depth+1))

return answer
31 changes: 31 additions & 0 deletions merge-two-sorted-lists/hyedall.py
Comment thread
parkhojeong marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
https://leetcode.com/problems/merge-two-sorted-lists/description/
시간 복잡도 : O(n)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

n이 무엇인지 적어 두면 읽는 사람이 더 빨리 알 수 있을 것 같아요. 저는 list1 길이를 n, list2 길이를 m으로 두고 O(n + m)으로 적었는데, n을 두 리스트 노드 수의 합으로 정하셨다면 지금처럼 O(n)도 맞는 것 같습니다!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

n을 두 리스트의 전체 노드 수로 생각하고 작성했습니다! 말씀주신 것처럼 변수의 의미도 함께 적는 게 명확할 것 같네요. 감사합니다 :)

공간 복잡도 : O(1)
"""

# 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: ListNode | None, list2: ListNode | None) -> ListNode | None:

dummy = ListNode(0)
tail = dummy

while list1 and list2:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is not None 없이 while list1 and list2:로 쓰셨네요! 노드 객체는 val이 0이어도 참이고 None만 거짓이라 이렇게 써도 되는군요. 저는 is not None을 붙여서 조건이 길어졌는데, 이렇게 쓰는 게 더 간결한 것 같아요 👍🏻

if list1.val <= list2.val:
tail.next = list1
list1 = list1.next
else:
tail.next = list2
list2 = list2.next

tail = tail.next

tail.next = list1 or list2

return dummy.next
43 changes: 43 additions & 0 deletions word-search/hyedall.py

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

깔끔하게 풀어주셨네요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
https://leetcode.com/problems/word-search/description/

시간 복잡도 : O(m * n * 3^L * L)
공간 복잡도 : O(m * n + L^2)
"""

class Solution:
def exist(self, board: list[list[str]], word: str) -> bool:
n, m = len(board[0]), len(board)

# 상, 하, 좌, 우
directions = [(0, -1), (0, 1), (-1, 0), (1, 0)]

stack = []

# 시작점 탐색
for i in range(n):
for j in range(m):
if board[j][i] == word[0]:
stack.append((i, j, 0, {(i, j)}))

while stack:
x, y, index, visited = stack.pop()

if index == len(word) - 1:
return True

for dx, dy in directions:
nx = x + dx
ny = y + dy

if (0 <= nx < n) and (0 <= ny < m):
if (nx, ny) not in visited:
if board[ny][nx] == word[index + 1]:
stack.append((
nx,
ny,
index + 1,
visited | {(nx, ny)}
Comment thread
parkhojeong marked this conversation as resolved.
Resolved
))

return False
Loading