Leetcode1090. 受标签影响的最大值

思路:根据值从大到小排序,然后在加的时候判断是否达到标签上限即可,一开始想用字典做,但是题目说是集合却连续出现两个8,因此使用元组+SortedList进行解决

python 复制代码
class Solution:
    def largestValsFromLabels(self, values: list[int], labels: list[int], numWanted: int, useLimit: int) -> int:
        from sortedcontainers import SortedList
        from collections import defaultdict
        sorted_values = SortedList()
        for num in range(0, len(labels)):
            sorted_values.add((values[num], labels[num]))
        total_sum = 0
        label_dict = defaultdict(int)
        for value_label in reversed(sorted_values):
            value = value_label[0]
            label = value_label[1]
            if label_dict[label] < useLimit:
                if numWanted > 0:
                    numWanted -= 1
                    total_sum += value
                    label_dict[label] += 1
                else:
                    return total_sum
        return total_sum
相关推荐
2301_764441332 小时前
LISA时空跃迁分析,地理时空分析
数据结构·python·算法
014-code2 小时前
订单超时取消与库存回滚的完整实现(延迟任务 + 状态机)
java·开发语言
lly2024062 小时前
组合模式(Composite Pattern)
开发语言
游乐码3 小时前
c#泛型约束
开发语言·c#
Dontla3 小时前
go语言Windows安装教程(安装go安装Golang安装)(GOPATH、Go Modules)
开发语言·windows·golang
chushiyunen3 小时前
python rest请求、requests
开发语言·python
cTz6FE7gA3 小时前
Python异步编程:从协程到Asyncio的底层揭秘
python
铁东博客3 小时前
Go实现周易大衍筮法三变取爻
开发语言·后端·golang
baidu_huihui3 小时前
在 CentOS 9 上安装 pip(Python 的包管理工具)
开发语言·python·pip
南 阳3 小时前
Python从入门到精通day63
开发语言·python