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
相关推荐
大貔貅喝啤酒30 分钟前
Python Requests库教程
自动化测试·python·requests库
copyer_xyf1 小时前
LangChain 调用 LLM
后端·python·agent
copyer_xyf1 小时前
Prompt 组织管理
后端·python·agent
asdfg12589631 小时前
JavaBean是什么?怎么理解?有什么用途?
java·开发语言
dsyyyyy11012 小时前
JavaScript变量
开发语言·javascript·ecmascript
shimly1234562 小时前
python3 uvicorn 是啥?
python
z落落3 小时前
C#WinForm 窗体切换与窗体传值(登录跳转案例)+WinForm 窗体传值(从上往下传、从下往上传)
开发语言·windows·c#
CTA量化套保3 小时前
期货量化程序 time.sleep 卡死:天勤单线程与 deadline 替代
python·区块链
allway23 小时前
How to Echo Multiline to a File in Bash [3 Methods]
开发语言·chrome·bash
weixin_462446233 小时前
手把手教你用 Bash 脚本自动更新 /etc/hosts —— 自动绑定网卡 IP 与节点名
开发语言·tcp/ip·bash