调试中...
调试中...
题目描述
题目描述
题解
题解
提交记录
提交记录
代码
代码
测试用例
测试用例
测试结果
测试结果
中等
相关标签
相关企业
提示

以两个整数数组  values 和 labels 给定 n 个项的值和标签,并且给出两个整数 numWanted 和 useLimit

你的任务是从这些项中找到一个值的和 最大 的子集使得:

  • 项的数量 最多 为 numWanted
  • 相同标签的项的数量 最多 为 useLimit

返回最大的和。

 

示例 1:

输入:values = [5,4,3,2,1], labels = [1,1,2,2,3], numWanted = 3, useLimit = 1

输出:9

解释:

选择的子集是第一个、第三个和第五个项,其值之和为 5 + 3 + 1。

示例 2:

输入:values = [5,4,3,2,1], labels = [1,3,3,3,2], numWanted = 3, useLimit = 2

输出:12

解释:

选择的子集是第一个、第二个和第三个项,其值之和为 5 + 4 + 3。

示例 3:

输入:values = [9,8,8,7,6], labels = [0,0,0,1,1], numWanted = 3, useLimit = 1

输出:16

解释:

选择的子集是第一个和第四个项,其值之和为 9 + 7。

 

提示:

  • n == values.length == labels.length
  • 1 <= n <= 2 * 104
  • 0 <= values[i], labels[i] <= 2 * 104
  • 1 <= numWanted, useLimit <= n
通过次数
26.1K
提交次数
38.9K
通过率
67.1%


相关企业

提示 1
Consider the items in order from largest to smallest value, and greedily take the items if they fall under the use_limit. We can keep track of how many items of each label are used by using a hash table.

评论 (0)

贡献者
© 2025 领扣网络(上海)有限公司
0 人在线
行 1,列 1
运行和提交代码需要登录
values =
[5,4,3,2,1]
labels =
[1,1,2,2,3]
numWanted =
3
useLimit =
1
Source