Skip to content

Commit 7c95f89

Browse files
authored
Merge pull request #2139 from changhyumm/main
[changhyumm] WEEK 04 solutions
2 parents 763c4dd + f0eab02 commit 7c95f89

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def findMin(self, nums: List[int]) -> int:
3+
# ์‹œ๊ฐ„๋ณต์žก๋„ O(logn) ์ œ์•ฝ์‚ฌํ•ญ์ด ์žˆ์œผ๋ฏ€๋กœ binary search ์‚ฌ์šฉ
4+
left = 0
5+
right = len(nums) - 1
6+
# rotated sort ์ด๋ฏ€๋กœ mid์™€ right ๋น„๊ต
7+
# mid ์™€ left ๋น„๊ต์‹œ ์ตœ์†Œ๊ฐ’์ด ์–ด๋”จ๋Š”์ง€ ํŠน์ •ํ•  ์ˆ˜๊ฐ€ ์—†์Œ (์˜ˆ์™ธ์ผ€์ด์Šค ๋ฐœ์ƒ)
8+
while left < right:
9+
mid = (left + right) // 2
10+
if nums[mid] < nums[right]:
11+
right = mid
12+
else:
13+
left = mid + 1
14+
return nums[left]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def maxDepth(self, root: Optional[TreeNode]) -> int:
9+
# root๊ฐ€ ์—†๋Š”๊ฒฝ์šฐ depth 0
10+
if root is None:
11+
return 0
12+
# depth๋ฅผ ๊ฐ™์ด ํฌํ•จ์‹œํ‚ด
13+
# dfs์˜ ๊ฒฝ์šฐ ๊ฐ ๊ฒฝ๋กœ๋ณ„๋กœ ๋๊นŒ์ง€ ํƒ์ƒ‰ํ•˜๋Š”๋ฐ, ๊ฐ ๊ฒฝ๋กœ๋งˆ๋‹ค depth๊ฐ€ ๋‹ค๋ฅด๋ฏ€๋กœ ํ‘œ๊ธฐํ•˜๋ฉด์„œ stack์— ์ถ”๊ฐ€ํ•ด์•ผํ•จ
14+
stack = [[root, 1]]
15+
max_depth = 1
16+
while stack:
17+
node, cur_depth = stack.pop()
18+
max_depth = max(max_depth, cur_depth)
19+
if node.left:
20+
stack.append([node.left, cur_depth + 1])
21+
if node.right:
22+
stack.append([node.right, cur_depth + 1])
23+
return max_depth
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
8+
# head of merged linked list๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๋ฏ€๋กœ head๋ฅผ ์ž„์‹œ๋กœ ์ƒ์„ฑ
9+
head = ListNode()
10+
pointer = head
11+
12+
# ์ •๋ ฌ๋œ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์ด๋ฏ€๋กœ ํ•˜๋‚˜๋ผ๋„ ๋๊นŒ์ง€ pointer๋ฅผ ์ด๋™ํ•ด์„œ None์ด ๋ ๋•Œ๊นŒ์ง€ loop
13+
# ์‹œ๊ฐ„๋ณต์žก๋„ O(n), ๊ณต๊ฐ„๋ณต์žก๋„ O(1)
14+
while list1 and list2:
15+
# val ๋น„๊ตํ•ด์„œ next ์ง€์ •
16+
if list1.val <= list2.val:
17+
pointer.next = list1
18+
list1 = list1.next
19+
else:
20+
pointer.next = list2
21+
list2 = list2.next
22+
# pointer๋„ next๋กœ ์ด๋™
23+
pointer = pointer.next
24+
# ๋‚จ์€ ๋ฆฌ์ŠคํŠธ์˜ ๊ฒฝ์šฐ ์ •๋ ฌ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ๊ทธ๋Œ€๋กœ ์—ฐ๊ฒฐ
25+
if list1:
26+
pointer.next = list1
27+
else:
28+
pointer.next = list2
29+
30+
return head.next

โ€Žword-search/changhyumm.pyโ€Ž

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def exist(self, board: List[List[str]], word: str) -> bool:
3+
rows, cols = len(board), len(board[0])
4+
visited = set()
5+
6+
def dfs(row, col, idx):
7+
# ๋๊นŒ์ง€ ํƒ์ƒ‰์‹œ ๊ธธ์ด์™€ k๊ฐ€ ๊ฐ™์•„์ง€๋ฏ€๋กœ True ๋ฐ˜ํ™˜
8+
if idx == len(word):
9+
return True
10+
# ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚œ ๊ฒฝ์šฐ, ๋ฐฉ๋ฌธํ•œ ๊ฒฝ์šฐ, ๊ฐ™์€ word๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ False ๋ฐ˜ํ™˜
11+
if not (0 <= row < rows) or not (0 <= col < cols) or (row, col) in visited or board[row][col] != word[idx]:
12+
return False
13+
14+
visited.add((row, col))
15+
res = dfs(row + 1, col, idx + 1) or dfs(row - 1, col, idx + 1) or dfs(row, col + 1, idx + 1) or dfs(row, col - 1, idx + 1)
16+
visited.remove((row, col))
17+
return res
18+
# ์‹œ์ž‘์  ํƒ์ƒ‰
19+
for row in range(rows):
20+
for col in range(cols):
21+
if dfs(row, col, 0):
22+
return True
23+
# ์‹œ๊ฐ„๋ณต์žก๋„ O(rows*cols*4^(word))
24+
# ๊ณต๊ฐ„๋ณต์žก๋„ O(rows*cols)
25+
return False

0 commit comments

Comments
ย (0)