当前位置:网站首页>LeetCode_6141_合并相似的物品
LeetCode_6141_合并相似的物品
2022-08-07 13:11:00 【Fitz1318】
题目链接
题目描述
给你两个二维整数数组 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 <= 1000items1[i].length == items2[i].length == 21 <= valuei, weighti <= 1000items1中每个valuei都是 唯一的 。items2中每个valuei都是 唯一的 。
解题思路
使用一个数组存储两个数组的值,然后一次放入list中,这样因为下标从小到大,所以不用排序
AC代码
class Solution {
public List<List<Integer>> mergeSimilarItems(int[][] items1, int[][] items2) {
int[] map = new int[2001];
for (int[] a : items1) {
map[a[0]] += a[1];
}
for (int[] b : items2) {
map[b[0]] += b[1];
}
List<List<Integer>> ans = new ArrayList<>();
for (int i = 0; i < 2001; i++) {
List<Integer> tmp = new ArrayList<>();
if (map[i] != 0) {
tmp.add(i);
tmp.add(map[i]);
ans.add(tmp);
}
}
return ans;
}
}
边栏推荐
- 8. cuBLAS Development Guide Chinese version--cublasGetMatrix() and cublasSetMatrix() in cuBLAS
- 目标检测工具箱MMDetection安装及使用示例
- 在Visual Studio Code中配置代码片段,方便用快捷键生成代码模板
- postgresql逻辑备份工具pg_dump和pg_resotre学习
- 微信小程序和微信H5有什么区别?
- 实现Sprite的Circle Fill效果及打了图集出现的问题
- 插帧中grid_sample函数详解
- 使用OpenCV测量图像中物体的大小
- China University of Petroleum (Beijing) - Online Work of the First Phase of "Percolation Mechanics"
- 由GEE生成逐月MODIS的NDVI影像
猜你喜欢

2022年广东省安全员A证第三批(主要负责人)考试模拟100题及在线模拟考试

网上复制代码的坑

DiskGenius Installation Tutorial

2022/8/5 拓扑排序+dp

代码随想录笔记_动态规划_518零钱兑换II

xss过滤绕过笔记

Navicat gets stuck for a few seconds after connecting to mysql for a period of time

亿纬锂能首件搭载自主研发46系列大圆柱电池系统产品成功下线

FairMOT Online Tracking Framework and Code Learning Records

蓝图传值给Niagara
随机推荐
Leetcode 47. 全排列 II
NestedTensor
LeetCode high-frequency question: longest common subsequence, playing game A and game B, how many prizes can the two brothers get by adding up
我用开天平台做了一个天气预报系统【开天aPaaS大作战】
5.cuBLAS开发指南中文版--cuBLAS中的Create()和Destroy()
传统网站与数字化网站的对比
China University of Petroleum (Beijing) - Online Work of the First Phase of "Percolation Mechanics"
Spire.Office for NET family bucket latest version 7.7.6
MySQL数据表中带OR的多条件查询
7. CuBLAS development in the Chinese version - cuBLAS guide cublasSetVector () and cublasGetVector ()
图像特效显示(上)
由GEE生成逐月MODIS的NDVI影像
excel sumifs多条件求和
在一个页面实现数据库的增删改查
5. Chinese version of cuBLAS Development Guide--Create() and Destroy() in cuBLAS
校园篮球网页作业成品 运动系列NBA篮球主题 学校篮球网页制作模板 学生简单体育运动网站设计成品
Installation and usage example of target detection toolbox MMDetection
2022年T电梯修理复习题及在线模拟考试
使用vxe-table行合并后所合并到的行后面会多出一列空白出来,
Custom types: structs, enums, unions