Repository navigation
[hyedall] WEEK 04 Solutions #2971
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
base: main
Are you sure you want to change the base?
Changes from all commits
ab025f2
73e03b7
f62e7aa
121b7ac
4b488eb
b3cf740
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| 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 |
| 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. n이 무엇인지 적어 두면 읽는 사람이 더 빨리 알 수 있을 것 같아요. 저는 list1 길이를 n, list2 길이를 m으로 두고 O(n + m)으로 적었는데, n을 두 리스트 노드 수의 합으로 정하셨다면 지금처럼 O(n)도 맞는 것 같습니다!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)} | ||
|
parkhojeong marked this conversation as resolved.
Resolved
|
||
| )) | ||
|
|
||
| return False | ||
Uh oh!
There was an error while loading. Please reload this page.