Roles that never meet
- O(n²) · Very hard
- Full plan
- Python
- JavaScript
- sets
- lists
- sorting
Problem
Marisol Quintanar is directing the play at the culture house and she is short of actors. The way out is for one person to take two roles, but that only works if those two roles are never together in a scene: nobody can be on stage twice at the same time.
Write a function that takes scenes, a list where each scene is the list of the roles that appear in it, in lowercase, with no accents and with no repeats inside a scene. The roles of the play are the ones that appear in some scene. Return the list of every pair of different roles that never meet in a scene. Each pair is a list with its two roles in alphabetical order, and the pairs come sorted too: first by the role that comes first and, if that one is the same, by the other.
With three scenes, ["witch", "wizard"], ["wizard", "queen"] and ["queen", "judge"], one person can play the judge and the witch, the judge and the wizard, or the queen and the witch, so you return [["judge", "witch"], ["judge", "wizard"], ["queen", "witch"]].
If no pair can be put together, you return an empty list.
Examples
The example
[["witch", "wizard"], ["wizard", "queen"], ["queen", "judge"]] → [["judge", "witch"], ["judge", "wizard"], ["queen", "witch"]]
Everyone in the same scene
[["granny", "mailman", "doctor"]] → []
Each role in its own scene
[["clown"], ["grocer"], ["soldier"]] → [["clown", "grocer"], ["clown", "soldier"], ["grocer", "soldier"]]
No scenes
[] → []
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def compatible_roles(scenes):
passJavaScript
function compatibleRoles(scenes) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.