-
Notifications
You must be signed in to change notification settings - Fork 64
Maple: Sabrina Lauredan #39
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,5 +8,35 @@ def possible_bipartition(dislikes): | |
| Time Complexity: ? | ||
| Space Complexity: ? | ||
| """ | ||
| pass | ||
| queue = deque() | ||
| set_a = set() | ||
| set_b = set() | ||
| visited = set() | ||
|
|
||
| if not dislikes: | ||
| return True | ||
| if dislikes[0]: | ||
| starting_node = 0 | ||
| else: | ||
| starting_node = 1 | ||
|
Comment on lines
+18
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔Consider an edge case where dislikes contains multiple nodes at the beginning of the adjacency list that are disconnected from the graph. How might you refactor your code to account for this situation? |
||
|
|
||
| queue.append(starting_node) | ||
| visited.add(starting_node) | ||
| set_a.add(starting_node) | ||
|
|
||
| while len(queue) > 0: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🐩 Nice BFS implementation |
||
| node = queue.popleft() | ||
| for i in dislikes[node]: | ||
| if i not in visited: | ||
| queue.append(i) | ||
| visited.add(i) | ||
| if node in set_a: | ||
| set_b.add(i) | ||
| else: | ||
| set_a.add(i) | ||
| else: | ||
| if node in set_a and i in set_a or node in set_b and i in set_b: | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
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.
⏱🪐 Time and space complexity?