Skip to content

Conversation

shubha-rajan
Copy link

No description provided.

Copy link

@CheezItMan CheezItMan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely works and I like the edge-cases you have in your tests. I had some minor quibbles and I was suspicious that it didn't handle all disconnected graphs, but it seems to do so. Nice work!

@@ -1,4 +1,43 @@
# I looked at the leetcode solution in Python
# (https://leetcode.com/problems/possible-bipartition/solution/)

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.


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|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor thing: You don't need to indent here.

Comment on lines 26 to 28
if queue.empty? && node < graph.keys.max
queue.push(node + 1)
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem like the most efficient way to handle a disconnected graph. Maybe also check to ensure that grouped[node].nil?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this to seek the next ungrouped node. It gets rid of a lot of the aimless wandering of the last approach, but still doesn't seem very efficient. Thoughts?:

if queue.empty? 
    graph.keys.each do |key|
       if grouped[key].nil?
           queue.push(key)
           break
       end
    end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants