【leetcode】1090. Largest Values From Labels
题目如下:
We have a set of items: the
i-th item has valuevalues[i]and labellabels[i].Then, we choose a subset
Sof 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
解题思路:贪心算法。每次取values中的最大值,如果对应labels没有超过限制,那么表示最大值可取;否则继续判断次大值。
代码如下:
class Solution(object):
def largestValsFromLabels(self, values, labels, num_wanted, use_limit):
"""
:type values: List[int]
:type labels: List[int]
:type num_wanted: int
:type use_limit: int
:rtype: int
"""
res = 0
def cmpf(v1,v2):
if v1[0] - v2[0] != 0:
return v2[0] - v1[0]
return v2[1] - v1[1]
val_list = sorted(zip(values,labels),cmp=cmpf) dic = {}
inx = 0
while num_wanted > 0 and inx < len(val_list):
v,l = val_list[inx]
if l not in dic or dic[l] < use_limit:
res += v
dic[l] = dic.setdefault(l,0) + 1
num_wanted -= 1
inx += 1
return res
【leetcode】1090. Largest Values From Labels的更多相关文章
- 【LeetCode】764. Largest Plus Sign 解题报告(Python)
[LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- [LeetCode] 1090. Largest Values From Labels
使用 Java 爬取 LeetCode 题目内容以及提交的AC代码 传送门 Description We have a set of items: the i-th item has value va ...
- 【LeetCode】952. Largest Component Size by Common Factor 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetco ...
- 【Leetcode】179. Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- 【LeetCode】976. Largest Perimeter Triangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- 【LeetCode】949. Largest Time for Given Digits 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】368. Largest Divisible Subset 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/largest-d ...
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
随机推荐
- 树的基本概念以及java实现二叉树
树具有的特点有: (1)每个结点有零个或多个子结点 (2)没有父节点的结点称为根节点 (3)每一个非根结点有且只有一个父节点 (4)除了根结点外,每个子结点可以分为多个不相交的子树. 树的基本术语 ...
- log4j.properties 输出指定类日志
比如,我只要众多日志中,红色框的日志,则可以指定类:com.dangdang.ddframe.rdb.sharding.parser.SQLParserFactory 修改配置文件: 再次输出结果为:
- Delphi7中ClientDataSet的排序
http://eteda.iteye.com/blog/1141312 Delphi7中ClientDataSet的排序 博客分类: Delphi 1.控件ClientDataSet的属性Inde ...
- Java 基础-IO、stream 流、文件操作
输入输出流的分类 在 java.io 包中,包含了输入输出操作所需的类. I/O 流可以安装不同的标准分类: 按照流的方向分类: 输入流:将信息从代码外部输入代码 输出流:将代码得到的数据输出到文件. ...
- ios平台appstore不支持网页内嵌app解决方案
苹果一直拒绝 UIWebView 内嵌 HTML5 页面的 iPhone.iPad APP应用上架到 App Store,建议这样的APP去做成Safari的Web应用.但是,苹果的审核人员只从界面. ...
- 事件驱动体系结构(EDA)
- little-endian And big-endian
coming from http://zhidao.baidu.com/link?url=B_7AA_O6TkCVlKw9t_Xifu6TzpaFUiDEVkH1iTRT4vUGD0uRmazwduf ...
- Netty内存池ByteBuf 内存回收
内存池ByteBuf 内存回收: 在前面的章节中我们有提到, 堆外内存是不受JVM 垃圾回收机制控制的, 所以我们分配一块堆外内存进行ByteBuf 操作时, 使用完毕要对对象进行回收, 本节就以Po ...
- 实现简单的计算器(设计UI)
要点说明: 1.一个textedit控件,其余全部是button控件,button布局方式:栅格布局(Grid layout) 2.对窗体的Title进行修改(默认是工程名) 3.在ui文件中设计的U ...
- poj 2248 Addition Chains (迭代加深搜索)
[题目描述] An addition chain for n is an integer sequence with the following four properties: a0 = 1 am ...