[LeetCode] 1090. Largest Values From Labels
使用 Java 爬取 LeetCode 题目内容以及提交的AC代码
Description
We have a set of items: the i
-th item has value values[i]
and label labels[i]
.
Then, we choose a subset S
of these items, such that:
|S| <= num_wanted
- For every label
L
, the number of items inS
with labelL
is<= use_limit
.
Return the largest possible sum of the subset S
.
Example 1:
Input: values = [5,4,3,2,1], labels = [1,1,2,2,3], num_wanted = 3, use_limit = 1
Output: 9
Explanation: The subset chosen is the first, third, and fifth item.
Example 2:
Input: values = [5,4,3,2,1], labels = [1,3,3,3,2], num_wanted = 3, use_limit = 2
Output: 12
Explanation: The subset chosen is the first, second, and third item.
Example 3:
Input: values = [9,8,8,7,6], labels = [0,0,0,1,1], num_wanted = 3, use_limit = 1
Output: 16
Explanation: The subset chosen is the first and fourth item.
Example 4:
Input: values = [9,8,8,7,6], labels = [0,0,0,1,1], num_wanted = 3, use_limit = 2
Output: 24
Explanation: The subset chosen is the first, second, and fourth item.
Note:
1 <= values.length == labels.length <= 20000
0 <= values[i], labels[i] <= 20000
1 <= num_wanted, use_limit <= values.length
思路
题意:选取一个子集,子集中元素的总个数不大于 num_wanted,对于 label 值相同的元素,选取的个数不能大于 use_limit(如use_limit = 2,label值为 1 的元素有4个,最多选两个),这样的子集要求其 value 总和最大
题解:贪心,按照元素的value值排序,数值大的先选
static const auto io_sync_off = []()
{
// turn off sync
std::ios::sync_with_stdio(false);
// untie in/out streams
std::cin.tie(nullptr);
return nullptr;
}(); struct Node {
int value, label; bool operator < (const Node &node)const{
return value > node.value;
}
}; class Solution {
public: int largestValsFromLabels(vector<int> &values, vector<int> &labels, int num_wanted, int use_limit) {
int size = values.size();
Node node[size + 5];
int sum = 0;
map<int, int>mp;
map<int, int>::iterator it;
for (int i = 0; i < size; i++) {
node[i].value = values[i];
node[i].label = labels[i];
}
sort(node, node + size);
for (int i = 0; i < size && num_wanted; i++) {
if (mp.find(node[i].label) != mp.end()) {
if (mp[node[i].label] < use_limit) {
sum += node[i].value;
mp[node[i].label]++;
num_wanted--;
}
} else {
mp[node[i].label] = 1;
sum += node[i].value;
num_wanted--;
}
}
return sum;
}
};
[LeetCode] 1090. Largest Values From Labels的更多相关文章
- 【leetcode】1090. Largest Values From Labels
题目如下: We have a set of items: the i-th item has value values[i] and label labels[i]. Then, we choose ...
- LeetCode 84. Largest Rectangle in Histogram 单调栈应用
LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- [LeetCode][Python]Largest Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/largest ...
- [LeetCode] Kth Largest Element in an Array 数组中第k大的数字
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- JavaScript中sort方法的一个坑(leetcode 179. Largest Number)
在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...
- LeetCode Kth Largest Element in an Array
原题链接在这里:https://leetcode.com/problems/kth-largest-element-in-an-array/ 题目: Find the kth largest elem ...
- Leetcode:Largest Number详细题解
题目 Given a list of non negative integers, arrange them such that they form the largest number. For e ...
- [LeetCode] Find Largest Value in Each Tree Row 找树每行最大的结点值
You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...
随机推荐
- 解决在linux下安装centos自带的mysql后,出现navicat远程连接失败的问题
最近在学习关于数据库相关的东西,所以下午尝试在linux下自己搭建了myql,我的mysql是直接安装centos系统自带的,安装过程不再赘述 安装完成后,从linux后台登录也显示成功,但是就是从n ...
- Spring IOC -bean对象的生命周期详解
生命周期执行的过程如下:1) spring对bean进行实例化,默认bean是单例2) spring对bean进行依赖注入3) 如果bean实现了BeanNameAware接口,spring将bean ...
- ZROI 19.08.11模拟赛
传送门 写在前面:为了保护正睿题目版权,这里不放题面,只写题解. dlstql,wsl A \(10pts:\) \(a=100,T=100\),对每个排列构造一个反的,一步到位即可. \(20pts ...
- Css min-height max-height min-width max-height
Css min-height应用地方解释我们有时设置一个对象盒子时候避免对象没有内容时候不能撑开,但内容多少不能确定所以又不能固定高度,这个时候我们就会需要css来设置min-height最小高度撑高 ...
- Python 列表(List)Ⅰ
Python 列表(List) 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型 ...
- c# 操作mysql数据库的时候会出现 插入中文汉字变成问号?
场景: 在mysql ce里面执行时没有问题的. c#操作会出现问号. 原因是: 链接字符串的时候 要设置Charset=utf8; 不然就会按默认的服务器设置编码,通常会出问题. 检查: 1.创建 ...
- jquery contains选择器 语法
jquery contains选择器 语法 作用::contains 选择器选取包含指定字符串的元素.该字符串可以是直接包含在元素中的文本,或者被包含于子元素中.经常与其他元素/选择器一起使用,来选择 ...
- jquery lt选择器 语法
jquery lt选择器 语法 作用::lt 选择器选取带有小于指定 index 值的元素.index 值从 0 开始.经常与其他元素/选择器一起使用,来选择指定的组中特定序号之前的元素(如上面的例子 ...
- pycharm如何添加固定代码块
1. file -- settings -- 搜索框输入live,找到 Live Templates 2. 选择你要添加到哪个语言中去,打开python组,并点击右上角 “+”,选择 1.Live T ...
- SQL create file遇到操作系统错误5拒绝访问
当在sql server 2014创建一个数据库时出现错误:尝试打开或创建物理文件 'G:\Test.mdf' 时,CREATE FILE 遇到操作系统错误 5(拒绝访问). 原因及解决方法如下: 这 ...