The block party
- O(2ⁿ) · Legendary
- Full plan
- Python
- JavaScript
- recursion
- lists
- loops
Problem
Don Chema is throwing the Christmas party for the whole block, and he wants as many neighbors as possible to come. The trouble is the feuds:
if two feuding neighbors are at the party together, an argument breaks out. From each feuding pair he can invite one of the two, or neither, but never both.
Write a function that takes feuds, a list with one entry per neighbor (neighbors are numbered from 0, and there are at most 12): feuds[i] is the list of neighbors that neighbor i is feuding with. Feuds go both ways: if j is in feuds[i], then i is in feuds[j]. Nobody feuds with themselves. Return a whole number: the most neighbors he can invite skip two feuding ones being there together.
With [[1], [0, 2], [1, 3], [2]] the feuds form a chain: 0 with 1, 1 with 2, and 2 with 3. He can invite 0 and 2, or 0 and 3, or 1 and 3, but not three of them: it returns 2. With [[1, 2], [0, 2], [0, 1]] all three are feuding with each other and he can only invite one: it returns 1. If there are no neighbors, the list is empty and it returns 0.
Examples
Feuds in a chain
[[1], [0, 2], [1, 3], [2]] → 2
Three feuding with each other
[[1, 2], [0, 2], [0, 1]] → 1
One is feuding with everyone
[[1, 2, 3], [0], [0], [0]] → 3
Nobody is feuding
[[], [], [], [], []] → 5
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def block_party(feuds):
passJavaScript
function blockParty(feuds) {
}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.