- 
                Notifications
    
You must be signed in to change notification settings  - Fork 16
 
Shubha 🧙♀️ #9
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?
Shubha 🧙♀️ #9
Changes from 7 commits
e5a659c
              bd0edd6
              b11e762
              99f9491
              2a014fd
              8b4b249
              f42560b
              1232714
              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 | 
|---|---|---|
| @@ -1,4 +1,43 @@ | ||
| # I looked at the leetcode solution in Python | ||
| # (https://leetcode.com/problems/possible-bipartition/solution/) | ||
| # to get an idea of how to turn the list of dislikes into a graph, | ||
| # but chose to implement a breadth-first solution instead | ||
| 
     | 
||
| def possible_bipartition(dislikes) | ||
| raise NotImplementedError, "possible_bipartition isn't implemented yet" | ||
| return true if dislikes.empty? | ||
| graph = build_graph(dislikes) | ||
| # start with an arbitrary node of the graph, | ||
| # assuming all nodes have at least one edge | ||
| grouped = {graph.keys[0] => true} | ||
| queue = [graph.keys[0]] | ||
| # using a bfs approach, set all neighbor nodes to the opposite label | ||
| # and add them to the queue if they haven't been visited | ||
| # return false if a neighbor has the same label as the current node | ||
| while !queue.empty? | ||
| node = queue.shift() | ||
| graph[node].each do |neighbor| | ||
| if grouped[neighbor] == nil | ||
| grouped[neighbor] = !grouped[node] | ||
| queue.push(neighbor) | ||
| elsif grouped[neighbor] == grouped[node] | ||
| return false | ||
| end | ||
| end | ||
| if queue.empty? && node < graph.keys.max | ||
| queue.push(node + 1) | ||
| end | ||
                
       | 
||
| end | ||
| return true | ||
| end | ||
| 
     | 
||
| 
     | 
||
| def build_graph(dislikes) | ||
| nodes = Hash.new {|h,k| h[k] = [] } #had to look up how to do this on StackOverflow | ||
| dislikes.each do |dislike| | ||
                
       | 
||
| dislike.each do |node| | ||
| nodes[node] += dislike - [node] | ||
| end | ||
| end | ||
| return nodes | ||
| end | ||
| 
     | 
||
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.
Ok great, it's been a while since you saw graphs in class anyway.