【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 ...
随机推荐
- DAY 6 上午
如果不是割点,答案减少2(n-1) 如果删去割点,删去之后整个图分成多个连通块 每一个联通块的大小*其他连通块的大小之和 先求出缩点之后的树 加尽可能少的边使树变成一个边双 找出树上的所有叶子节点(度 ...
- 十五、jenkins环境配置
1. jenkins包下载,下载地址:https://jenkins.io/download/ 版本:Jenkins 2.134,下载war包 2. JDK下载:下载地址:http://www.ora ...
- XML读写工具类
摘要:①读取XML文件,生成pojo对象:②将对象信息保存到xml中. 步骤: ①新建一个普通的java类BasePage: package com.test.selenium.pages; impo ...
- UI自动化之特殊处理二(弹框\下拉框\选项\文件上传)
弹框\下拉框\选项\文件上传也是一些比较特殊的操作 目录 1.弹框 2.下拉框 3.选项 4.文件上传 1.弹框 弹框有三种形式,value为alert.confirm.prompt三种的弹框,第一个 ...
- protel封装总结(新手必看)
零件封装是指实际零件焊接到电路板时所指示的外观和焊点的位置.是纯粹的空间概念.因此不同的元件可共用同一零件封装,同种元件也可有不同的零件封装.像电阻,有传统的针插式,这种元件体积较大,电路板必须钻孔才 ...
- fastboot烧录镜像--VTS&GSI镜像替换
fastboot简介 Android提供的原生工具,主要用于替换镜像. 源码在SDK工程中,/system/core/fastboot目录下 安卓分区&镜像 见链接分区和映像--google官 ...
- js 获取屏幕宽高
网页可见区域宽: document.body.clientWidth 网页可见区域高: document.body.clientHeight 网页可见区域宽: document.body.offset ...
- Vue 基础 day03
定义Vue 组件 什么是组件:组件的出现,就是为了拆分 Vue 实例的代码量,能够让我们以不同的组件,来划分不同的功能模块,将来我们需要什么样的功能,就可以去调用对应的组件: 组件化和模块化的不同: ...
- package.json说明
package.json是什么? 直接的说:就是管理你本地安装的npm包 一个package.json文件可以做如下事情: 展示项目所依赖的npm包 允许你指定一个包的版本[范围] 让你建立起稳定,意 ...
- nodejs、npm、 typescript、angular-cli安装
一.node.js环境安装 1.从Node.js官网下载对应平台的安装程序,进行安装,在Windows上安装时务必选择全部组件,包括勾选Add to Path. 2.安装完成后,打开window命令行 ...