【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 ...
随机推荐
- 高通Camera bring up软件流程【转】
本文转载自:http://blog.csdn.net/liwei16611/article/details/51279658 高通camera bring up分为两种类型:YUV和bayerbrin ...
- 裸BFS题若干
1poj 3278 http://poj.org/problem?id=3278 #include<math.h> #include<algorithm> #include&l ...
- drf 视图源码详解
目录 mixin类和Generic类 CreateModelMixin 创建 ListModelMixin - 查看多条数据 RetrieveModelMixin 获取单条数据 UpdateModel ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_06 Properties集合_1_使用Properties集合存储数据,遍历取出集合中的数据
map下面的实现类叫做Hashtable Properties是唯一和IO流相结合的 讲解 代码
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_05 IO字符流_3_字符输出流_Writer类&FileWriter类
- Numpy的补充(重要!!)
轴的概念 英文解释 https://www.sharpsightlabs.com/blog/numpy-axes-explained/ 汉化解释 https://www.jianshu.com/p/ ...
- IPv4首部
<图解TCP/IP> 4.7 IPv4的首部 版本:由4比特构成,表示标识IP首部的版本号.IPv4的版本号即为4,因此在这个字段上的值也为“4”. 首部长度:由4比特构成,表明IP首部的 ...
- 几个模拟OLE事件注册、调用的宏
最近遇到一个要求,将原来的OCX控件,替换成直接的DLL调用. 遇到OLE的事件回调,写了三个宏,用于简化代码 #define OLE_ENVENT_IN_CLASS_ONE(event_name, ...
- 浅谈vue单页面seo问题
最近做项目的时候,被要求做seo,由于项目已经开发完毕,且只需首页做seo,所以考虑再三,决定用prerender-spa-plugin结合vue-meta-info来实现首页的seo.如果你的页面是 ...
- Ecshop二次开发必备基础
EcShop二次开发学习方法 近年来,随着互联网的发展,电子商务也跟着一起成长,B2B,C2C,B2C的电子商务模式也不断的成熟.这时催生出了众多电子商务相关的PHP开源产品.B2C方面有Ecshop ...