Skip to content

Commit 0e40cc9

Browse files
authored
Merge pull request #2483 from ohkingtaek/main
[ohkingtaek] WEEK 04 Solutions
2 parents dd62e82 + 6338bf0 commit 0e40cc9

4 files changed

Lines changed: 91 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def findMin(self, nums: List[int]) -> int:
3+
"""
4+
์‹œ๊ฐ„๋ณต์žก๋„: O(n)
5+
๊ณต๊ฐ„๋ณต์žก๋„: O(1)
6+
์™œ ์ด๋ ‡๊ฒŒ ํ•ด๋„ solve๊ฐ€ ๋˜๋„ค์š”..ใ…Žใ…Ž
7+
"""
8+
return min(nums)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
"""
10+
์‹œ๊ฐ„๋ณต์žก๋„: O(n)
11+
๊ณต๊ฐ„๋ณต์žก๋„: O(n)
12+
์ด์ง„ ํŠธ๋ฆฌ ํƒ์ƒ‰ํ•˜๋ฉฐ ๋ށ์Šค ์ฆ๊ฐ€ ๊ณ„์† ํ•˜๋ฉฐ ์ง„ํ–‰
13+
"""
14+
depth = 0
15+
if not root:
16+
return 0
17+
array = [(root, 1)]
18+
while array:
19+
node, d = array.pop()
20+
depth = max(d, depth)
21+
if node.left:
22+
array.append((node.left, d + 1))
23+
if node.right:
24+
array.append((node.right, d + 1))
25+
return depth
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
"""
9+
์‹œ๊ฐ„๋ณต์žก๋„: O(n)
10+
๊ณต๊ฐ„๋ณต์žก๋„: O(1)
11+
1. ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•ฉ์ณ์„œ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ ๋ฐ˜ํ™˜
12+
2. ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋Œ๋ฉด์„œ ์ž‘์€ ๊ฐ’์„ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€
13+
3. ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ชจ๋‘ ์ˆœํšŒํ•˜๋ฉด ๋‚จ์€ ๊ฒƒ์„ ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€
14+
"""
15+
root = ListNode(None)
16+
node = root
17+
while list1 and list2:
18+
if list1.val < list2.val:
19+
node.next = list1
20+
list1 = list1.next
21+
else:
22+
node.next = list2
23+
list2 = list2.next
24+
node = node.next
25+
node.next = list1 or list2
26+
return root.next

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution:
2+
def exist(self, board: List[List[str]], word: str) -> bool:
3+
"""
4+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(M * N * 4^L)
5+
๊ณต๊ฐ„ ๋ณต์žก๋„: O(M * N)
6+
1. ํ–‰๋ ฌ์„ ์ˆœํšŒํ•˜๋ฉฐ ๋‹จ์–ด์˜ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž์™€ ์ผ์น˜ํ•˜๋Š” ์œ„์น˜๋ฅผ ์ฐพ๊ธฐ
7+
2. ์ฐพ์€ ์œ„์น˜์—์„œ ์ƒํ•˜์ขŒ์šฐ๋กœ ์ด๋™ํ•˜๋ฉฐ ๋‹จ์–ด์˜ ๋‹ค์Œ ๋ฌธ์ž์™€ ์ผ์น˜ํ•˜๋Š” ์œ„์น˜๋ฅผ ์ฐพ๊ธฐ
8+
3. ๋‹จ์–ด์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๊นŒ์ง€ ์ฐพ์œผ๋ฉด True๋ฅผ ๋ฐ˜ํ™˜, ๋ชป ์ฐพ์œผ๋ฉด False
9+
"""
10+
m, n = len(board), len(board[0])
11+
12+
def dfs(i, j, k):
13+
if i < 0 or i >= m or j < 0 or j >= n or board[i][j] != word[k]:
14+
return False
15+
if k == len(word) - 1:
16+
return True
17+
t = board[i][j]
18+
board[i][j] = "#"
19+
dx, dy = (1, -1, 0, 0), (0, 0, 1, -1)
20+
ok = False
21+
for d in range(4):
22+
if dfs(i + dx[d], j + dy[d], k + 1):
23+
ok = True
24+
break
25+
board[i][j] = t
26+
return ok
27+
28+
for i in range(m):
29+
for j in range(n):
30+
if dfs(i, j, 0):
31+
return True
32+
return False

0 commit comments

Comments
ย (0)