Created
April 25, 2024 21:44
-
-
Save Gerst20051/27e5917e4dd2e4a945a5963bf659729b to your computer and use it in GitHub Desktop.
Secret Meetings
This file contains 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
# /* | |
# There are N people | |
# Some subset of the N people know the secret | |
# There are M meetings which are encoded as subsets of the N people | |
# There is a total order over the meetings that tells you the order in which they occur | |
# When people meet they share the secret | |
# Calculate the set of people who know the secret after all the meetings | |
# */ | |
initial_set = [1,2] | |
meetings = [ | |
[6], | |
[1,2,3], | |
[3,4], | |
[5] | |
] | |
def people_who_know_secret(initial_set, meetings) | |
people_that_know_secret = initial_set | |
for meeting in meetings | |
if !people_that_know_secret.intersection(meeting).empty? | |
people_that_know_secret = (people_that_know_secret + meeting).uniq | |
end | |
end | |
people_that_know_secret | |
end | |
people_that_know_the_secret = people_who_know_secret(initial_set, meetings) | |
puts people_that_know_the_secret # [1,2,3,4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment