Skip to content

Commit 7b3c0e5

Browse files
authored
Merge pull request #1952 from hu6r1s/main
[hu6r1s] WEEK 14 Solutions
2 parents 5640684 + 4da6e92 commit 7b3c0e5

File tree

7 files changed

+162
-0
lines changed

7 files changed

+162
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
from collections import deque
8+
9+
class Solution:
10+
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
11+
if not root:
12+
return []
13+
14+
queue = deque([root])
15+
result = []
16+
while queue:
17+
tmp = []
18+
for _ in range(len(queue)):
19+
node = queue.popleft()
20+
tmp.append(node.val)
21+
22+
if node.left:
23+
queue.append(node.left)
24+
if node.right:
25+
queue.append(node.right)
26+
result.append(tmp)
27+
return result

counting-bits/hu6r1s.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def countBits(self, n: int) -> List[int]:
3+
ans = []
4+
for i in range(n+1):
5+
ans.append(bin(i)[2:].count("1"))
6+
return ans

graph-valid-tree/hu6r1s.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
if len(edges) != n - 1:
2+
return False
3+
4+
graph = [[] for _ in range(n)]
5+
for node, adj in edges:
6+
graph[node].append(adj)
7+
graph[adj].append(node)
8+
9+
visited = set()
10+
11+
def dfs(node):
12+
visited.add(node)
13+
for adj in graph[node]:
14+
if adj not in visited:
15+
dfs(adj)
16+
17+
dfs(0)
18+
return len(visited) == n

house-robber-ii/hu6r1s.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def rob(self, nums: List[int]) -> int:
3+
return max(nums[0], self.helper(nums[1:]), self.helper(nums[:-1]))
4+
5+
6+
def helper(self, nums):
7+
rob1, rob2 = 0, 0
8+
for num in nums:
9+
new_rob = max(rob1 + num, rob2)
10+
rob1 = rob2
11+
rob2 = new_rob
12+
return rob2

meeting-rooms-ii/hu6r1s.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
from heapq import heappush, heappop
16+
17+
class Solution:
18+
"""
19+
@param intervals: an array of meeting time intervals
20+
@return: the minimum number of conference rooms required
21+
"""
22+
def min_meeting_rooms(self, intervals: List[Interval]) -> int:
23+
# Write your code here
24+
intervals.sort()
25+
ends = []
26+
for start, end in intervals:
27+
if ends and ends[0] <= start:
28+
heappop(ends)
29+
heappush(ends, end)
30+
return len(ends)

number-of-islands/hu6r1s.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from collections import deque
2+
3+
class Solution:
4+
"""
5+
문제를 보니 바로 그래프 탐색이 떠올라서 bfs 알고리즘을 사용해서 구현
6+
백준 문제에서 많이 풀어보던건데 너무 오래 되어 계속 헷갈렸음
7+
다시 공부해야함
8+
"""
9+
def numIslands(self, grid: List[List[str]]) -> int:
10+
def bfs(grid, i, j):
11+
queue = deque()
12+
queue.append([i, j])
13+
grid[i][j] = "0"
14+
while queue:
15+
x, y = queue.popleft()
16+
for k in range(4):
17+
nx = x + dx[k]
18+
ny = y + dy[k]
19+
if nx < 0 or nx >= n or ny < 0 or ny >= m:
20+
continue
21+
if grid[nx][ny] == "0":
22+
continue
23+
grid[nx][ny] = "0"
24+
queue.append([nx, ny])
25+
26+
27+
dx = [-1, 1, 0, 0]
28+
dy = [0, 0, -1, 1]
29+
n, m = len(grid), len(grid[0])
30+
cnt = 0
31+
for i in range(n):
32+
for j in range(m):
33+
if grid[i][j] == "1":
34+
bfs(grid, i, j)
35+
cnt += 1
36+
return cnt

word-search-ii/hu6r1s.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution:
2+
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
3+
n, m = len(board), len(board[0])
4+
res = set()
5+
6+
trie = {}
7+
for word in words:
8+
node = trie
9+
for ch in word:
10+
node = node.setdefault(ch, {})
11+
node['$'] = word
12+
13+
def dfs(x, y, node):
14+
ch = board[x][y]
15+
if ch not in node:
16+
return
17+
nxt = node[ch]
18+
19+
if '$' in nxt:
20+
res.add(nxt['$'])
21+
22+
board[x][y] = "#"
23+
for dx, dy in [(1,0), (-1,0), (0,1), (0,-1)]:
24+
nx, ny = x + dx, y + dy
25+
if 0 <= nx < n and 0 <= ny < m and board[nx][ny] != "#":
26+
dfs(nx, ny, nxt)
27+
board[x][y] = ch
28+
29+
for i in range(n):
30+
for j in range(m):
31+
dfs(i, j, trie)
32+
33+
return list(res)

0 commit comments

Comments
 (0)