-
-
Notifications
You must be signed in to change notification settings - Fork 193
[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
Merged
+73
−0
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a371a2e
Solve : Counting Bits
printjin-gmailcom 8aa3e1c
Solve : meeting rooms ii
printjin-gmailcom cc9a30c
Merge branch 'main' of https://github.com/printjin-gmailcom/Dalle_Cod…
printjin-gmailcom d2bfffb
Solve : Word Search II
printjin-gmailcom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
단어만 찾으면 된다고만 생각해서, Trie 구조를 정리(clean-up)하는 과정까지는 미처 생각하지 못했어요. 한참 찾아보면서 이 부분이 왜 중요한지 알게 되었고, 덕분에 새로운 개념을 배웠습니다. 👍