2025-01-09 20:29:41 +08:00
< p > You are given < code > n< / code > item' s value and label as two integer arrays < code > values< / code > and < code > labels< / code > . You are also given two integers < code > numWanted< / code > and < code > useLimit< / code > .< / p >
2022-03-27 20:37:52 +08:00
2025-01-09 20:29:41 +08:00
< p > Your task is to find a subset of items with the < strong > maximum sum< / strong > of their values such that:< / p >
2022-03-27 20:37:52 +08:00
< ul >
2025-01-09 20:29:41 +08:00
< li > The number of items is < strong > at most< / strong > < code > numWanted< / code > .< / li >
< li > The number of items with the same label is < strong > at most< / strong > < code > useLimit< / code > .< / li >
2022-03-27 20:37:52 +08:00
< / ul >
2025-01-09 20:29:41 +08:00
< p > Return the maximum sum.< / p >
2022-03-27 20:37:52 +08:00
< p > < / p >
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 1:< / strong > < / p >
2022-03-27 20:37:52 +08:00
2025-01-09 20:29:41 +08:00
< div class = "example-block" >
< p > < strong > Input:< / strong > < span class = "example-io" > values = [5,4,3,2,1], labels = [1,1,2,2,3], numWanted = 3, useLimit = 1< / span > < / p >
< p > < strong > Output:< / strong > < span class = "example-io" > 9< / span > < / p >
< p > < strong > Explanation:< / strong > < / p >
< p > The subset chosen is the first, third, and fifth items with the sum of values 5 + 3 + 1.< / p >
< / div >
2022-03-27 20:37:52 +08:00
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 2:< / strong > < / p >
2022-03-27 20:37:52 +08:00
2025-01-09 20:29:41 +08:00
< div class = "example-block" >
< p > < strong > Input:< / strong > < span class = "example-io" > values = [5,4,3,2,1], labels = [1,3,3,3,2], numWanted = 3, useLimit = 2< / span > < / p >
< p > < strong > Output:< / strong > < span class = "example-io" > 12< / span > < / p >
< p > < strong > Explanation:< / strong > < / p >
< p > The subset chosen is the first, second, and third items with the sum of values 5 + 4 + 3.< / p >
< / div >
2022-03-27 20:37:52 +08:00
2023-12-09 18:42:21 +08:00
< p > < strong class = "example" > Example 3:< / strong > < / p >
2022-03-27 20:37:52 +08:00
2025-01-09 20:29:41 +08:00
< div class = "example-block" >
< p > < strong > Input:< / strong > < span class = "example-io" > values = [9,8,8,7,6], labels = [0,0,0,1,1], numWanted = 3, useLimit = 1< / span > < / p >
< p > < strong > Output:< / strong > < span class = "example-io" > 16< / span > < / p >
< p > < strong > Explanation:< / strong > < / p >
< p > The subset chosen is the first and fourth items with the sum of values 9 + 7.< / p >
< / div >
2022-03-27 20:37:52 +08:00
< p > < / p >
< p > < strong > Constraints:< / strong > < / p >
< ul >
< li > < code > n == values.length == labels.length< / code > < / li >
< li > < code > 1 < = n < = 2 * 10< sup > 4< / sup > < / code > < / li >
< li > < code > 0 < = values[i], labels[i] < = 2 * 10< sup > 4< / sup > < / code > < / li >
< li > < code > 1 < = numWanted, useLimit < = n< / code > < / li >
< / ul >