Skip to content

Commit 5d41a1c

Browse files
authored
Merge pull request #1630 from yyyyyyyyyKim/main
[yyyyyyyyyKim] WEEK 14 solutions
2 parents 38d72c9 + 9cb3659 commit 5d41a1c

File tree

5 files changed

+190
-0
lines changed

5 files changed

+190
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
8+
from collections import deque
9+
10+
class Solution:
11+
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
12+
13+
# BFS(ํ ์‚ฌ์šฉ)
14+
# ์‹œ๊ฐ„๋ณต์žก๋„ O(n), ๊ณต๊ฐ„๋ณต์žก๋„ O(n)
15+
16+
if not root:
17+
return []
18+
19+
# ํ์— root๋…ธ๋“œ ์ถ”๊ฐ€(์ดˆ๊ธฐํ™”)
20+
q = deque([root])
21+
answer = []
22+
23+
# ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ํƒ์ƒ‰(๋ชจ๋“  ๋…ธ๋“œ ํƒ์ƒ‰)
24+
while q:
25+
level = [] # ํ˜„์žฌ ๋ ˆ๋ฒจ์˜ ๋…ธ๋“œ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ
26+
27+
for _ in range(len(q)): # ํ˜„์žฌ ๋ ˆ๋ฒจ์— ์žˆ๋Š” ๋…ธ๋“œ ์ˆ˜๋งŒํผ ๋ฐ˜๋ณต
28+
node = q.popleft() # ํ์—์„œ ๋…ธ๋“œ ๊บผ๋‚ด์„œ
29+
level.append(node.val) # level์— ์ €์žฅ
30+
31+
# ์ž์‹ ๋…ธ๋“œ๋“ค์ด ์žˆ๋‹ค๋ฉด ํ์— ์ถ”๊ฐ€
32+
if node.left:
33+
q.append(node.left)
34+
if node.right:
35+
q.append(node.right)
36+
37+
# ํ˜„์žฌ ๋ ˆ๋ฒจ answer์— ์ถ”๊ฐ€
38+
answer.append(level)
39+
40+
return answer

โ€Žcounting-bits/yyyyyyyyyKim.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def countBits(self, n: int) -> List[int]:
3+
# ์‹œ๊ฐ„๋ณต์žก๋„ O(n), ๊ณต๊ฐ„๋ณต์žก๋„ O(n)
4+
5+
# 0์œผ๋กœ ์ดˆ๊ธฐํ™”
6+
answer = [0]*(n+1)
7+
8+
for i in range(1,n+1):
9+
# i//2(๋งˆ์ง€๋ง‰๋น„ํŠธ๋ฅผ์ œ์™ธํ•œ๋ถ€๋ถ„)์˜ 1์˜ ๊ฐœ์ˆ˜ + i์˜ ๋งˆ์ง€๋ง‰๋น„ํŠธ(ํ™€์ˆ˜๋ฉด 1, ์ง์ˆ˜๋ฉด 0)
10+
answer[i] = answer[i//2] + (i&1)
11+
12+
return answer

โ€Žhouse-robber-ii/yyyyyyyyyKim.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def rob(self, nums: List[int]) -> int:
3+
4+
# DP
5+
# ์‹œ๊ฐ„๋ณต์žก๋„ O(n^2), ๊ณต๊ฐ„๋ณต์žก๋„ O(n)
6+
7+
# ์ง‘์ด 1๊ฐœ~3๊ฐœ์ธ ๊ฒฝ์šฐ, ๊ฐ€์žฅ ํฐ ์ง‘๋งŒ ํ„ธ๊ธฐ
8+
if len(nums) <= 3:
9+
return max(nums)
10+
11+
# ์ฒซ ๋ฒˆ์งธ ์ง‘์„ ํฌํ•จํ•˜์ง€ ์•Š๊ณ  ํ„ธ๊ธฐ
12+
dp1 = [0]*len(nums)
13+
dp1[1] = nums[1]
14+
dp1[2] = nums[2]
15+
for i in range(2, len(nums)):
16+
dp1[i] = max(dp1[i-1], max(dp1[:i-1])+nums[i])
17+
18+
# ๋งˆ์ง€๋ง‰ ์ง‘์„ ํฌํ•จํ•˜์ง€ ์•Š๊ณ  ํ„ธ๊ธฐ
19+
dp2 = [0]*len(nums)
20+
dp2[0] = nums[0]
21+
dp2[1] = nums[1]
22+
for i in range(2, len(nums)-1):
23+
dp2[i] = max(dp2[i-1], max(dp2[:i-1])+nums[i])
24+
25+
return max(max(dp1), max(dp2))

โ€Žmeeting-rooms-ii/yyyyyyyyyKim.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from typing import (
2+
List,
3+
)
4+
from lintcode import (
5+
Interval,
6+
)
7+
8+
"""
9+
Definition of Interval:
10+
class Interval(object):
11+
def __init__(self, start, end):
12+
self.start = start
13+
self.end = end
14+
"""
15+
16+
class Solution:
17+
"""
18+
@param intervals: an array of meeting time intervals
19+
@return: the minimum number of conference rooms required
20+
"""
21+
def min_meeting_rooms(self, intervals: List[Interval]) -> int:
22+
23+
# ํˆฌํฌ์ธํ„ฐ
24+
# ์‹œ๊ฐ„๋ณต์žก๋„ O(n log n) , ๊ณต๊ฐ„๋ณต์žก๋„ O(n)
25+
26+
# ์‹œ์ž‘ ์‹œ๊ฐ„๊ณผ ์ข…๋ฃŒ ์‹œ๊ฐ„์„ ๊ฐ๊ฐ ์ •๋ ฌ
27+
s = sorted([i.start for i in intervals])
28+
e = sorted([i.end for i in intervals])
29+
30+
s_pointer = e_pointer = 0
31+
rooms = max_rooms = 0
32+
33+
while s_pointer < len(intervals):
34+
35+
# ๊ธฐ์กด ํšŒ์˜๊ฐ€ ๋๋‚˜๊ธฐ ์ „์— ์ƒˆ๋กœ์šด ํšŒ์˜ ์‹œ์ž‘(์ƒˆ๋กœ์šด ๋ฐฉ ํ•„์š”)
36+
if s[s_pointer] < e[e_pointer]:
37+
rooms += 1
38+
s_pointer += 1
39+
# ๊ธฐ์กด ํšŒ์˜ ์ข…๋ฃŒ(๋ฐฉ ํ‡ด์‹ค)
40+
else:
41+
rooms -= 1
42+
e_pointer += 1
43+
44+
# ์ง€๊ธˆ๊นŒ์ง€ ํ•„์š”ํ•œ ๋ฐฉ์˜ ์ตœ๋Œ€๊ฐ’์„ ๊ฐฑ์‹ 
45+
max_rooms = max(max_rooms, rooms)
46+
47+
return max_rooms
48+

โ€Žword-search-ii/yyyyyyyyyKim.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class TrieNode:
2+
def __init__(self):
3+
self.children = {} # ํ˜„์žฌ ๋ฌธ์ž์—์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋‹ค์Œ ๋ฌธ์ž๋“ค ์ €์žฅ
4+
self.word = None # ์ด๋…ธ๋“œ์—์„œ ๋๋‚˜๋Š” ๋‹จ์–ด๊ฐ€ ์žˆ๋‹ค๋ฉด ์ €์žฅ
5+
6+
class Solution:
7+
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
8+
9+
# ๋‹ค์‹œํ’€์–ด๋ณผ๊ฒƒ. ์–ด๋ ค์› ์Œ. Trie! ๋ณด๋“œ๋กœ ํƒ์ƒ‰ํ•ด์•ผํ•จ!(๋‹จ์–ด๋กœ ํƒ์ƒ‰ํ•˜๋Š”๊ฒŒ ์•„๋‹˜!)
10+
# DFS(๋ฐฑํŠธ๋ž˜ํ‚น), trie
11+
# DFS๋กœ๋งŒ ํ’€๋ฉด TLE(์‹œ๊ฐ„์ดˆ๊ณผ)๋œธ. trie๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•ด์•ผํ•จ.
12+
13+
# Trie๋ฃจํŠธ๋…ธ๋“œ ์ƒ์„ฑ
14+
root = TrieNode()
15+
16+
# words ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ Trie ์ƒ์„ฑ
17+
for word in words:
18+
node = root
19+
20+
for i in word:
21+
22+
if i not in node.children:
23+
node.children[i] = TrieNode()
24+
node = node.children[i]
25+
26+
node.word = word # ๋‹จ์–ด ๋์—์„œ word ์ €์žฅ
27+
28+
answer = []
29+
30+
rows, cols = len(board), len(board[0])
31+
32+
# DFS
33+
def dfs(x, y, node):
34+
c = board[x][y]
35+
36+
# ํ˜„์žฌ ๋ฌธ์ž๊ฐ€ ํŠธ๋ผ์ด์— ์—†์œผ๋ฉด ๋ฐ”๋กœ ์ข…๋ฃŒ
37+
if c not in node.children:
38+
return
39+
40+
next_node = node.children[c]
41+
42+
# ๋‹จ์–ด๋ฅผ ์ฐพ์œผ๋ฉด answer์— ์ถ”๊ฐ€ํ•˜๊ณ , ์ค‘๋ณต๋ฐฉ์ง€์œ„ํ•ด None์ฒ˜๋ฆฌ
43+
if next_node.word:
44+
answer.append(next_node.word)
45+
next_node.word = None # ์ค‘๋ณต๋ฐฉ์ง€
46+
47+
board[x][y] = '#' # ํ˜„์žฌ์œ„์น˜ '#'๋กœ ๋ฐฉ๋ฌธํ‘œ์‹œ
48+
49+
# ์ƒํ•˜์ขŒ์šฐ DFS ํƒ์ƒ‰
50+
for dx, dy in [(-1,0),(1,0),(0,-1),(0,1)]:
51+
nx, ny = x + dx, y + dy
52+
# ๋ฒ”์œ„ ๋‚ด์— ์žˆ๊ณ , ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์œ„์น˜๋ผ๋ฉด DFS ํƒ์ƒ‰
53+
if 0 <= nx < rows and 0 <= ny < cols and board[nx][ny] != '#':
54+
dfs(nx, ny, next_node)
55+
56+
# DFS ์ข…๋ฃŒ ํ›„, ์›๋ž˜ ๋ฌธ์ž๋กœ ๋ณต๊ตฌ(๋ฐฑํŠธ๋ž˜ํ‚น)
57+
board[x][y] = c # ์›์ƒ๋ณต๊ตฌ
58+
59+
60+
# DFS ์‹œ์ž‘์ (๋ชจ๋“  ๋ณด๋“œ ์นธ์„ ์‹œ์ž‘์ ์œผ๋กœ DFS ํƒ์ƒ‰)
61+
for i in range(rows):
62+
for j in range(cols):
63+
dfs(i, j, root)
64+
65+
return answer

0 commit comments

Comments
ย (0)