1090. 受标签影响的最大值
作者:互联网
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
"""
# 用字典,按标签统计所有值
mydict = {}
i = 0
n = len(labels)
while i < n:
if labels[i] in mydict.keys():
mydict[labels[i]].append(values[i])
else:
mydict[labels[i]] = [values[i]]
i += 1
# 遍历字典,每个标签中取出use_limit个数放到res中
res = []
for item in mydict.values():
item.sort(reverse=True)
res += item[:use_limit]
# res降序排,返回sum(res[:num_wanted])
res.sort(reverse=True)
return sum(res[:num_wanted])
标签:1090,int,标签,最大值,labels,num,values,res,mydict 来源: https://www.cnblogs.com/panweiwei/p/14024940.html