Skip to content
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

🌊 - Kim #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion lib/possible_bipartition.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,36 @@
# Time Complexity: O(n x m), where n is the number of puppies, m is the number of enemies
# Space Complexity:

def possible_bipartition(dislikes)
Comment on lines +1 to 4

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "possible_bipartition isn't implemented yet"
puppy_enemies = {}

dislikes.each_with_index do |puppies, index|
puppy_enemies[index] = puppies
end

puppy_groups = {}

dislikes.length.times do |puppy|
next if puppy_groups[puppy] # skip if group is already set

puppy_groups[puppy] = 'RED'

q = Queue.new
q.enq(puppy)

until q.empty?
current = q.deq

puppy_enemies[current].each do |enemy|
if puppy_groups[enemy].nil?
puppy_groups[enemy] = puppy_groups[current] == 'RED' ? 'BLUE' : 'RED'
q.enq(enemy)
elsif puppy_groups[enemy] == puppy_groups[current]
return false
end
end
end
end

return true
end