当前位置:网站首页>LeetCode简单题之合并相似的物品
LeetCode简单题之合并相似的物品
2022-08-10 12:05:00 【·星辰大海】
题目
给你两个二维整数数组 items1 和 items2 ,表示两个物品集合。每个数组 items 有以下特质:
items[i] = [valuei, weighti] 其中 valuei 表示第 i 件物品的 价值 ,weighti 表示第 i 件物品的 重量 。
items 中每件物品的价值都是 唯一的 。
请你返回一个二维数组 ret,其中 ret[i] = [valuei, weighti], weighti 是所有价值为 valuei 物品的 重量之和 。
注意:ret 应该按价值 升序 排序后返回。
示例 1:
输入:items1 = [[1,1],[4,5],[3,8]], items2 = [[3,1],[1,5]]
输出:[[1,6],[3,9],[4,5]]
解释:
value = 1 的物品在 items1 中 weight = 1 ,在 items2 中 weight = 5 ,总重量为 1 + 5 = 6 。
value = 3 的物品再 items1 中 weight = 8 ,在 items2 中 weight = 1 ,总重量为 8 + 1 = 9 。
value = 4 的物品在 items1 中 weight = 5 ,总重量为 5 。
所以,我们返回 [[1,6],[3,9],[4,5]] 。
示例 2:
输入:items1 = [[1,1],[3,2],[2,3]], items2 = [[2,1],[3,2],[1,3]]
输出:[[1,4],[2,4],[3,4]]
解释:
value = 1 的物品在 items1 中 weight = 1 ,在 items2 中 weight = 3 ,总重量为 1 + 3 = 4 。
value = 2 的物品在 items1 中 weight = 3 ,在 items2 中 weight = 1 ,总重量为 3 + 1 = 4 。
value = 3 的物品在 items1 中 weight = 2 ,在 items2 中 weight = 2 ,总重量为 2 + 2 = 4 。
所以,我们返回 [[1,4],[2,4],[3,4]] 。
示例 3:
输入:items1 = [[1,3],[2,2]], items2 = [[7,1],[2,2],[1,4]]
输出:[[1,7],[2,4],[7,1]]
解释:
value = 1 的物品在 items1 中 weight = 3 ,在 items2 中 weight = 4 ,总重量为 3 + 4 = 7 。
value = 2 的物品在 items1 中 weight = 2 ,在 items2 中 weight = 2 ,总重量为 2 + 2 = 4 。
value = 7 的物品在 items2 中 weight = 1 ,总重量为 1 。
所以,我们返回 [[1,7],[2,4],[7,1]] 。
提示:
1 <= items1.length, items2.length <= 1000
items1[i].length == items2[i].length == 2
1 <= valuei, weighti <= 1000
items1 中每个 valuei 都是 唯一的 。
items2 中每个 valuei 都是 唯一的 。
来源:力扣(LeetCode)
解题思路
将两个数组做成字典,其中每个数组中元素的第一个位置作为key第二个位置作为value,这样就可以直接取两个字典的并集,将存在的value都加在一起即可。
class Solution:
def mergeSimilarItems(self, items1: List[List[int]], items2: List[List[int]]) -> List[List[int]]:
items1,items2=dict(items1),dict(items2)
for i in items1.keys()|items2.keys():
items1[i]=items1.get(i,0)+items2.get(i,0)
return sorted(list(items1.items()))
边栏推荐
- 时间序列的数据分析(五):简单预测法
- 可视化服务编排在金融APP中的实践
- 部署项目半途而废后续
- Overseas media publicity. What problems should domestic media pay attention to?
- 吃透Chisel语言.36.Chisel实战之以FIFO为例(一)——FIFO Buffer和Bubble FIFO的Chisel实现
- 这三个 Go 水平自测题,你手写不出来还是先老实上班吧,过来看看
- 毕业总结
- 【集合】HashSet和ArrayList的查找Contains()时间复杂度
- Can CLIP also do segmentation tasks?The University of Göttingen proposed a model CLIPSeg that uses text and image prompts to perform three segmentation tasks at the same time, draining CLIP capabiliti
- CURRENT_TIMESTAMP(6) 函数是否存在问题?
猜你喜欢
16、Pytorch Lightning入门
第六届”蓝帽杯“全国大学生网络安全技能大赛半决赛部分WriteUp
你是怎么知道数据库 Htap 能力强弱的?怎么能看出来
【论文+代码】PEBAL/Pixel-wise Energy-biased Abstention Learning for Anomaly Segmentation on Complex Urban Driving Scenes(复杂城市驾驶场景异常分割的像素级能量偏置弃权学习)
郭晶晶家的象棋私教,好家伙是个机器人
多线程下自旋锁设计基本思想
「企业架构」应用架构概述
如何让别人看不懂你的 JS 代码?把你当大佬!
漏洞管理计划的未来趋势
Can CLIP also do segmentation tasks?The University of Göttingen proposed a model CLIPSeg that uses text and image prompts to perform three segmentation tasks at the same time, draining CLIP capabiliti
随机推荐
LeetCode 25. A set of K flipped linked lists
CodeForces - 628D (数位dp)
Deploy the project halfway through the follow-up
LeetCode 83. Remove Duplicate Elements in Sorted List
Golang分布式应用之etcd
Does face attendance choose face comparison 1:1 or face search 1:N?
Chapter9 : De Novo Molecular Design with Chemical Language Models
Chapter 5 virtual memory
[List merge] Combine multiple lists into one list
动态规划之最长回文子串
An enhanced dynamic packet buffer management. The core part of the paper
Guo Jingjing's personal chess teaching, the good guy is a robot
人脸考勤是选择人脸比对1:1还是人脸搜索1:N?
Drive IT Modernization with Low Code
dedecms支持Word内容一键导入
一文详解 implementation api embed
阿里云贾朝辉:云XR平台支持彼真科技呈现国风科幻虚拟演唱会
IM即时通讯开发WebSocket从入门到精通
mSystems | 中农汪杰组揭示影响土壤“塑料际”微生物群落的机制
什么是云流化?