The intersection of all combinations of n sets

前端 未结 2 578
执念已碎
执念已碎 2020-12-28 11:56

I need help finding an efficient algorithm to solve this problem:

Given n unsorted sets of integers, find all possible combinations of n and their intersections.

2条回答
  •  感情败类
    2020-12-28 12:12

    Using your example, just note that

    1 n 2 n 3

    is also

    (1 n 2) n 3

    So if you cache your (1 n 2) solution, you can reuse it, so that computing 1 n 2 n 3 will only require one additional intersection calculation. Generally, if there are, as in your example, four possible set combinations, then only four intersections will ultimately have to be done if you save and reuse prior results.

提交回复
热议问题