-
-
Notifications
You must be signed in to change notification settings - Fork 250
[hu6r1s] WEEK 14 Solutions #1952
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8a33c86
feat: Solve counting-bits problem
hu6r1s 8fefbeb
feat: Solve binary-tree-level-order-traversal problem
hu6r1s 7507b00
feat: Solve house-robber-ii problem
hu6r1s 5ff8112
feat: Solve meeting-rooms-ii problem
hu6r1s 4da6e92
feat: Solve word-search-ii problem
hu6r1s 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,27 @@ | ||
| # Definition for a binary tree node. | ||
| # class TreeNode: | ||
| # def __init__(self, val=0, left=None, right=None): | ||
| # self.val = val | ||
| # self.left = left | ||
| # self.right = right | ||
| from collections import deque | ||
|
|
||
| class Solution: | ||
| def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: | ||
| if not root: | ||
| return [] | ||
|
|
||
| queue = deque([root]) | ||
| result = [] | ||
| while queue: | ||
| tmp = [] | ||
| for _ in range(len(queue)): | ||
| node = queue.popleft() | ||
| tmp.append(node.val) | ||
|
|
||
| if node.left: | ||
| queue.append(node.left) | ||
| if node.right: | ||
| queue.append(node.right) | ||
| result.append(tmp) | ||
| 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: int) -> List[int]: | ||
| ans = [] | ||
| for i in range(n+1): | ||
| ans.append(bin(i)[2:].count("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,18 @@ | ||
| if len(edges) != n - 1: | ||
| return False | ||
|
|
||
| graph = [[] for _ in range(n)] | ||
| for node, adj in edges: | ||
| graph[node].append(adj) | ||
| graph[adj].append(node) | ||
|
|
||
| visited = set() | ||
|
|
||
| def dfs(node): | ||
| visited.add(node) | ||
| for adj in graph[node]: | ||
| if adj not in visited: | ||
| dfs(adj) | ||
|
|
||
| dfs(0) | ||
| return len(visited) == n |
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,12 @@ | ||
| class Solution: | ||
| def rob(self, nums: List[int]) -> int: | ||
| return max(nums[0], self.helper(nums[1:]), self.helper(nums[:-1])) | ||
|
|
||
|
|
||
| def helper(self, nums): | ||
| rob1, rob2 = 0, 0 | ||
| for num in nums: | ||
| new_rob = max(rob1 + num, rob2) | ||
| rob1 = rob2 | ||
| rob2 = new_rob | ||
| return rob2 |
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 @@ | ||
| from typing import ( | ||
| List, | ||
| ) | ||
| from lintcode import ( | ||
| Interval, | ||
| ) | ||
|
|
||
| """ | ||
| Definition of Interval: | ||
| class Interval(object): | ||
| def __init__(self, start, end): | ||
| self.start = start | ||
| self.end = end | ||
| """ | ||
| from heapq import heappush, heappop | ||
|
|
||
| class Solution: | ||
| """ | ||
| @param intervals: an array of meeting time intervals | ||
| @return: the minimum number of conference rooms required | ||
| """ | ||
| def min_meeting_rooms(self, intervals: List[Interval]) -> int: | ||
| # Write your code here | ||
| intervals.sort() | ||
| ends = [] | ||
| for start, end in intervals: | ||
| if ends and ends[0] <= start: | ||
| heappop(ends) | ||
| heappush(ends, end) | ||
| return len(ends) |
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,36 @@ | ||
| from collections import deque | ||
|
|
||
| class Solution: | ||
| """ | ||
| 문제를 보니 바로 그래프 탐색이 떠올라서 bfs 알고리즘을 사용해서 구현 | ||
| 백준 문제에서 많이 풀어보던건데 너무 오래 되어 계속 헷갈렸음 | ||
| 다시 공부해야함 | ||
| """ | ||
| def numIslands(self, grid: List[List[str]]) -> int: | ||
| def bfs(grid, i, j): | ||
| queue = deque() | ||
| queue.append([i, j]) | ||
| grid[i][j] = "0" | ||
| while queue: | ||
| x, y = queue.popleft() | ||
| for k in range(4): | ||
| nx = x + dx[k] | ||
| ny = y + dy[k] | ||
| if nx < 0 or nx >= n or ny < 0 or ny >= m: | ||
| continue | ||
| if grid[nx][ny] == "0": | ||
| continue | ||
| grid[nx][ny] = "0" | ||
| queue.append([nx, ny]) | ||
|
|
||
|
|
||
| dx = [-1, 1, 0, 0] | ||
| dy = [0, 0, -1, 1] | ||
| n, m = len(grid), len(grid[0]) | ||
| cnt = 0 | ||
| for i in range(n): | ||
| for j in range(m): | ||
| if grid[i][j] == "1": | ||
| bfs(grid, i, j) | ||
| cnt += 1 | ||
| return cnt |
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,33 @@ | ||
| class Solution: | ||
| def findWords(self, board: List[List[str]], words: List[str]) -> List[str]: | ||
| n, m = len(board), len(board[0]) | ||
| res = set() | ||
|
|
||
| trie = {} | ||
| for word in words: | ||
| node = trie | ||
| for ch in word: | ||
| node = node.setdefault(ch, {}) | ||
| node['$'] = word | ||
|
|
||
| def dfs(x, y, node): | ||
| ch = board[x][y] | ||
| if ch not in node: | ||
| return | ||
| nxt = node[ch] | ||
|
|
||
| if '$' in nxt: | ||
| res.add(nxt['$']) | ||
|
|
||
| board[x][y] = "#" | ||
| for dx, dy in [(1,0), (-1,0), (0,1), (0,-1)]: | ||
| nx, ny = x + dx, y + dy | ||
| if 0 <= nx < n and 0 <= ny < m and board[nx][ny] != "#": | ||
| dfs(nx, ny, nxt) | ||
| board[x][y] = ch | ||
|
|
||
| for i in range(n): | ||
| for j in range(m): | ||
| dfs(i, j, trie) | ||
|
|
||
| return list(res) |
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.
BFS 를 사용해서 풀이 진행해 주셨네요!
DFS 풀이도 도전해 보시면 좋을것 같아요.