​力扣解法汇总2363. 合并相似的物品

news/2024/7/7 21:28:51

目录链接:

力扣编程题-解法汇总_分享+记录-CSDN博客

GitHub同步刷题项目:

https://github.com/September26/java-algorithms

原题链接:力扣


描述:

给你两个二维整数数组 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 都是 唯一的 。

解题思路:

* 解题思路:
* 构建一个map,其中key为price,value为price和weight。
* 最后map的value转换成list,然后排序即可
 

代码:

public class Solution2363 {

    public List<List<Integer>> mergeSimilarItems(int[][] items1, int[][] items2) {
        Map<Integer, List<Integer>> map = new HashMap<>();
        put2Map(map, items1);
        put2Map(map, items2);
        List<List<Integer>> collect = map.values().stream().sorted(Comparator.comparingInt(o -> o.get(0))).collect(Collectors.toList());
        return collect;
    }

    private void put2Map(Map<Integer, List<Integer>> map, int[][] items1) {
        for (int[] item : items1) {
            int value = item[0];
            int weight = item[1];
            List<Integer> integers = map.get(value);
            if (integers == null) {
                integers = new ArrayList<>();
                map.put(value, integers);
            }
            if (integers.size() == 0) {
                integers.add(value);
                integers.add(weight);
            } else {
                Integer remove = integers.remove(1);
                integers.add(remove + weight);
            }
        }
    }
}


http://lihuaxi.xjx100.cn/news/950599.html

相关文章

【C++】-- 智能指针

目录 智能指针意义 智能指针的使用及原理 RAII 智能指针的原理 std::auto_ptr std::auto_ptr的模拟实现 std::unique_ptr std::unique_ptr模拟实现 std::shared_ptr std::shared_ptr的模拟实现 循环引用问题 智能指针意义 #问&#xff1a;为什么需要智能指针&#…

前端经典react面试题及答案

为什么 React 元素有一个 $$typeof 属性 目的是为了防止 XSS 攻击。因为 Synbol 无法被序列化&#xff0c;所以 React 可以通过有没有 $$typeof 属性来断出当前的 element 对象是从数据库来的还是自己生成的。 如果没有 $$typeof 这个属性&#xff0c;react 会拒绝处理该元素。…

P02 滴水逆向1月4号公开课

公开课进制转换汇编如何工作的逻辑运算二进制逻辑运算|| 运算 和 | 运算&& 运算 和 & 运算^(异或) 运算! (非)运算左移二进制逻辑运算应用加法运算运算与电脑硬件进行加密寄存器内存寻址范围使用内存寻址公式堆栈变形的艺术进制转换 二进制十六进制如何转换的 汇…

【组织架构】中国铁路太原局集团有限公司

1 公司简介 中国铁路太原局集团有限公司&#xff0c;是中国国家铁路集团有限公司管理的18个铁路局集团有限公司之一&#xff0c;简称“太局”。成立于2005年3月18日&#xff0c;共有职工11.5万人。 管辖南同蒲铁路、北同蒲铁路、大秦铁路、侯月铁路、石太铁路、侯西&#xff08…

即时通讯技术文集(第9期):Java NIO和Netty入门系列 [共19篇]

为了更好地分类阅读52im.net 总计1000多篇精编文章&#xff0c;我将在每周三推送新的一期技术文集&#xff0c;本次是第9 期。 [-1-] 少啰嗦&#xff01;一分钟带你读懂Java的NIO和经典IO的区别 [链接] http://www.52im.net/thread-2635-1-1.html [摘要] 在本文中&#xff0c…

推荐算法 - 汇总

本文主要对推荐算法整体知识点做汇总&#xff0c;做到总体的理解&#xff1b;深入理解需要再看专业的材料。推荐算法的意义推荐根据用户兴趣和行为特点&#xff0c;向用户推荐所需的信息或商品&#xff0c;帮助用户在海量信息中快速发现真正所需的商品&#xff0c;提高用户黏性…

Ubuntu20.04安装Cuckoo

参考链接&#xff1a; &#xff08;1&#xff09;【主要参考】http://www.wityx.com/post/134851_1_1.html &#xff08;2&#xff09;【主要参考】在Ubuntu18.04上搭建Cuckoo Sandbox2.0.7 https://www.jianshu.com/p/4dd6373fa206 &#xff08;3&#xff09;与Cuckoo的斗智斗…

单通道说话人语音分离——DPRNN(Dual-Path Recurrent Neural Network)

参考文献&#xff1a;《DUAL-PATH RNN: EFFICIENT LONG SEQUENCE MODELING FOR TIME-DOMAIN SINGLE-CHANNEL SPEECH SEPARATION》 DPRNN网络是Con-Tasnet的改进网络 Con-Tasnet介绍详情请看上一篇文章 单通道说话人语音分离——Conv-TasNet(Convolutional Time-domain audio…