[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 inSwith labelLis<= 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 <= 200000 <= values[i], labels[i] <= 200001 <= 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 ...
随机推荐
- C#其他知识
.NET理解为一个运行库环境和一个全面的基础类库. .NET三个关键实体(构造块):CLR. CTS. CLS 公共语言运行库层为CLR .功能:定位加载和管理.NET类型.也负责底层的工作如内存管理 ...
- java一键搭建新项目(地址)
构建地址: https://start.spring.io 文档地址:https://www.cnblogs.com/ityouknow/p/5662753.html
- Java注解Annotation与自定义注解详解
Java注解简介 开发中经常使用到注解,在项目中也偶尔会见到过自定义注解,今天就来探讨一下这个注解是什么鬼,以及注解的应用场景和如何自定义注解. 下面列举开发中常见的注解 @Override:用于标识 ...
- MapReduce 中的两表 join 方案解析
1. 概述 在传统数据库(如:MYSQL)中,JOIN操作是非常常见且非常耗时的.而在HADOOP中进行JOIN操作,同样常见且耗时,由于Hadoop的独特设计思想,当进行JOIN操作时,有一些特殊的 ...
- scala spark(2.10)读取kafka(2.11_1.0.0)示例
1.pom加载jar包 <dependency> <groupId>org.apache.spark</groupId> <artifactId>spa ...
- java——AtomicInteger 中 incrementAndGet与getAndIncrement 两个方法的区别
https://blog.csdn.net/chenkaibsw/article/details/81031950 源码: getAndIncrement: public final int getA ...
- String,Integer,int类型之间的相互转换
String, Integer, int 三种类型之间可以两两进行转换 1. 基本数据类型到包装数据类型的转换 int -> Integer (两种方法) Integer it1 = new I ...
- 使用 nodejs 和 axios 以及 cherrio 爬取天气预报
安装依赖 引入依赖 发送请求 解析请求的返回值 以下代码可以复制直接运行,获得 7 天的天气预报 const axios = require('axios') const cheerio = requ ...
- VirtualBox NAT Host-only模式下,自动分配IP上网。
修改宿主机上,virtualbox自建虚拟网卡Host-Only 2. 因为我使用了两个适配器,所以这两个适配器的名字分别是ifcfg-eth0, ifcfg-eth1. ifcfg-eh0一般默认就 ...
- linux 目录介绍
/boot: 系统启动相关的文件,如内核.initrd,以及grub(bootloader)/dev: 设备文件 /etc:配置文件/home:用户的家目录,每一个用户的家目录通常默认为/home/U ...