当前位置:网站首页>Merge similar items in LeetCode simple questions
Merge similar items in LeetCode simple questions
2022-08-10 12:56:00 【·The sea of stars】
题目
gives you two 2D integer arrays items1 和 items2 ,Represents a collection of two items.每个数组 items Has the following characteristics:
items[i] = [valuei, weighti] 其中 valuei 表示第 i 件物品的 价值 ,weighti 表示第 i 件物品的 重量 .
items The value of each item in is 唯一的 .
请你返回一个二维数组 ret,其中 ret[i] = [valuei, weighti], weighti are all values for valuei 物品的 sum of weights .
注意:ret Should be by value 升序 排序后返回.
示例 1:
输入:items1 = [[1,1],[4,5],[3,8]], items2 = [[3,1],[1,5]]
输出:[[1,6],[3,9],[4,5]]
解释:
value = 1 's items are in items1 中 weight = 1 ,在 items2 中 weight = 5 ,总重量为 1 + 5 = 6 .
value = 3 items again items1 中 weight = 8 ,在 items2 中 weight = 1 ,总重量为 8 + 1 = 9 .
value = 4 's items are in 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 's items are in items1 中 weight = 1 ,在 items2 中 weight = 3 ,总重量为 1 + 3 = 4 .
value = 2 's items are in items1 中 weight = 3 ,在 items2 中 weight = 1 ,总重量为 3 + 1 = 4 .
value = 3 's items are in 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 's items are in items1 中 weight = 3 ,在 items2 中 weight = 4 ,总重量为 3 + 4 = 7 .
value = 2 's items are in items1 中 weight = 2 ,在 items2 中 weight = 2 ,总重量为 2 + 2 = 4 .
value = 7 's items are in 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)
解题思路
Make two arrays into a dictionary,where the first position of the element in each array is taken askeyThe second position asvalue,This way you can directly take the union of the two dictionaries,将存在的valueJust add them all together.
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()))

边栏推荐
- LT8911EXB MIPI CSI/DSI转EDP信号转换
- 【数字IC验证进阶】SoC系统验证和IP模块验证的区别及侧重点分析
- Does face attendance choose face comparison 1:1 or face search 1:N?
- 培训机构学习费用是多少呢?
- 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
- CV复习:空洞卷积
- H264 码率控制
- Dining (网络流)
- 可视化服务编排在金融APP中的实践
- LeetCode中等题之颠倒字符串中的单词
猜你喜欢

「网络架构」网络代理第一部分: 代理概述

Does face attendance choose face comparison 1:1 or face search 1:N?

Data Analysis of Time Series (5): Simple Prediction Method

一文详解 implementation api embed

IM即时通讯开发WebSocket从入门到精通

关于flask中static_folder 和 static_url_path参数理解

ASP.NET Core依赖注入系统学习教程:ServiceDescriptor(服务注册描述类型)

CLIP还能做分割任务?哥廷根大学提出一个使用文本和图像prompt,能同时作三个分割任务的模型CLIPSeg,榨干CLIP能力...

「企业架构」应用架构概述

Alibaba Cloud Jia Zhaohui: Cloud XR platform supports Bizhen Technology to present a virtual concert of national style sci-fi
随机推荐
毕业总结
海外邮件发送指南(二)
Golang分布式应用之etcd
dedecms supports one-click import of Word content
第六届”蓝帽杯“全国大学生网络安全技能大赛半决赛部分WriteUp
Servlet---Solve the problem of Chinese garbled characters in post requests
查看 CUDA cudnn 版本 & 测试 cuda 和 cudnn 有效性「建议收藏」
16、Pytorch Lightning入门
es6-promise对象详解
加密游戏:游戏的未来
实践为主,理论为辅!腾讯大佬MySQL高阶宝典震撼来袭!
浮动及其特点
啥?他一个人写了个价值100万的软件,却用来开源了!
娄底疾控中心实验室设计理念说明
LeetCode中等题之比较版本号
“68道 Redis+168道 MySQL”精品面试题(带解析)
基于PLECS的离网(孤岛)并联逆变器的Droop Control下垂控制仿真
海外媒体宣发.国内媒体发稿要注意哪些问题?
正则表达式常用示例
Accumulated and thin hair!Safety Dog has once again obtained the certification of scientific and technological achievements transformation!