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
相关推荐
geovindu18 分钟前
CSharp: Breadth First Search Algorithm and Depth First Search Algorithm
开发语言·后端·算法·c#·.net·搜索算法
RSTJ_162528 分钟前
PYTHON+AI LLM DAY ONE HUNDRED AND EIGHTEEN
python
库克克1 小时前
【C++】set 与multiset
开发语言·c++
Wang's Blog1 小时前
Go-Zero项目开发34: 微服务超时控制与重试机制实践
开发语言·微服务·golang·go-zero
tkevinjd2 小时前
MiniCode 项目详解7:Memory 记忆系统
大数据·python·搜索引擎·llm·agent
郭老二2 小时前
【Python】Web框架 FastAPI 详解
python·fastapi
DFT计算杂谈2 小时前
交错磁研究进展材料物性与交叉应用
数据库·人工智能·python·opencv·算法
敖行客 Allthinker2 小时前
docker容器安装Python反推镜像步骤(适用于临时调试用)
python·docker·容器
2zcode2 小时前
项目文档:基于MATLAB神经网络的心力衰竭预测与临床辅助决策系统研究
开发语言·神经网络·matlab·心力衰竭预测\
rrrjqy2 小时前
夏普比率与 Beta——Quant-for-Beginners 量化入门Task7
python·金融