Your input is a non-empty set of non-empty sets of positive integers. For example:
{{1}, {2, 3}, {2,4}, {1,4}}
Your task is to output the maximal number of elements that can be picked from the sets with the following conditions:
- You can only pick at most 1 element from a set
- You can't pick the same element from 2 different sets
In this example you can pick 1
from the first set, 3
from the second set, 2
from the third set and 4
from the last set, for a total of 4 elements.
Io is flexible. You can take a list, instead of sets, etc.
There are no gaps in the numbers (eg. {{2,3},{3,4}}
is not a valid input). Or in other words, the input flattened must contain all numbers [1,n]
where n is the largest number in the input.
This is code-golf, so shortest code wins.
Examples
{{1}}
-> 1
{{1}, {2, 3}, {2,4}, {1,4}}
-> 4
{{1}, {2}, {1,2,3,4}}
-> 3
{{1}, {2}, {2,3}}
-> 3
{{1, 2, 3}, {2, 4, 5, 6}, {3, 5, 6, 7}, {1, 2}, {2, 3}, {1, 3}}
-> 5
{{1,2,3,4,5}, {1,2}, {2,3}, {1,3}}
-> 4
{{1,2},{1,3},{1,4},{2,3},{2,4},{3,4},{1,2,3},{1,2,4},{1,3,4},{2,3,4},{5,6,7,8,9,10}}
-> 5
{{1,4}, {2,3}, {1,3}, {1,2,3}}
-> 4
Background
Recently @AnishSharma posted this question on Stack Overflow. This is a slightly modified version of that question
3
from the second set,2
from the second set" a typo for "3
from the second set,2
from the third set"? \$\endgroup\$