Skip to content

[printjin-gmailcom] Week 14 Solutions #1628

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions binary-tree-level-order-traversal/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def levelOrder(self, root):
result = []
def dfs(node, level):
if not node:
return
if len(result) == level:
result.append([])
result[level].append(node.val)
dfs(node.left, level + 1)
dfs(node.right, level + 1)
dfs(root, 0)
return result
6 changes: 6 additions & 0 deletions counting-bits/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution:
def countBits(self, n):
ans = [0] * (n + 1)
for i in range(1, n + 1):
ans[i] = ans[i >> 1] + (i & 1)
return ans
10 changes: 10 additions & 0 deletions house-robber-ii/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def rob(self, nums):
if len(nums) == 1:
return nums[0]
def rob_linear(houses):
prev = curr = 0
for num in houses:
prev, curr = curr, max(curr, prev + num)
return curr
return max(rob_linear(nums[:-1]), rob_linear(nums[1:]))
14 changes: 14 additions & 0 deletions meeting-rooms-ii/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def minMeetingRooms(self, intervals):
if not intervals:
return 0
starts = sorted(i[0] for i in intervals)
ends = sorted(i[1] for i in intervals)
s = e = rooms = 0
while s < len(intervals):
if starts[s] < ends[e]:
rooms += 1
else:
e += 1
s += 1
return rooms
30 changes: 30 additions & 0 deletions word-search-ii/printjin-gmailcom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution:
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
trie = {}
for word in words:
node = trie
for char in word:
node = node.setdefault(char, {})
node["$"] = word
rows, cols = len(board), len(board[0])
result = []
def dfs(r, c, node):
char = board[r][c]
if char not in node:
return
next_node = node[char]
word = next_node.pop("$", None)
if word:
result.append(word)
board[r][c] = "#"
for dr, dc in [(-1,0),(1,0),(0,-1),(0,1)]:
nr, nc = r + dr, c + dc
if 0 <= nr < rows and 0 <= nc < cols and board[nr][nc] != "#":
dfs(nr, nc, next_node)
board[r][c] = char
if not next_node:
node.pop(char)
for r in range(rows):
for c in range(cols):
dfs(r, c, trie)
return result