简体   繁体   English

获取满足所有键的最少值

[英]Get fewest number of values that satisfies all keys

Say I have one of two setups假设我有两种设置之一

map[int][]int{
  19: {1, 2},
  20: {2, 3},
  233: {3}
}

or或者

map[int][]int{
  1: {19},
  2: {19, 20},
  3: {20, 233},
}

I would like to get an output that is the fewest number of values that satisfies the keys (in the first example)我想得到一个 output ,它是满足键的最少值(在第一个示例中)

There are two valid outputs for this example []int{1, 3} or []int{2,3}此示例有两个有效输出[]int{1, 3}[]int{2,3}

Another example:另一个例子:

In:在:

map[int][]int{
  18: {1}
  19: {1, 2},
  20: {2, 3},
  233: {3}
}

Out: []int{1,3}输出: []int{1,3}

I've looked into DFS, but can't seem to wrap my head around setting it up to be able to get the solution.我已经研究过 DFS,但似乎无法集中精力设置它以获得解决方案。 Maybe this could also be a set cover problem.也许这也可能是一个固定的封面问题。

Each value is equivalent to the subset of the keys that it will satisfy.每个值都等效于它将满足的键的子集。

You want the smallest set of values that cover all of the keys.您需要涵盖所有键的最小值集。

This is exactly the https://en.wikipedia.org/wiki/Set_cover_problem , which was one of the 21 original problems that were proven to be NP-complete.这正是https://en.wikipedia.org/wiki/Set_cover_problem ,这是被证明是 NP 完全的 21 个原始问题之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM