首页 > 其他分享 >leetcode-2363-easy

leetcode-2363-easy

时间:2022-11-07 20:02:21浏览次数:39  
标签:weight value occurs items2 item items1 easy 2363 leetcode

Merge Similar Items

You are given two 2D integer arrays, items1 and items2, representing two sets of items. Each array items has the following properties:

items[i] = [valuei, weighti] where valuei represents the value and weighti represents the weight of the ith item.
The value of each item in items is unique.
Return a 2D integer array ret where ret[i] = [valuei, weighti], with weighti being the sum of weights of all items with value valuei.

Note: ret should be returned in ascending order by value.

Example 1:

Input: items1 = [[1,1],[4,5],[3,8]], items2 = [[3,1],[1,5]]
Output: [[1,6],[3,9],[4,5]]
Explanation: 
The item with value = 1 occurs in items1 with weight = 1 and in items2 with weight = 5, total weight = 1 + 5 = 6.
The item with value = 3 occurs in items1 with weight = 8 and in items2 with weight = 1, total weight = 8 + 1 = 9.
The item with value = 4 occurs in items1 with weight = 5, total weight = 5.  
Therefore, we return [[1,6],[3,9],[4,5]].
Example 2:

Input: items1 = [[1,1],[3,2],[2,3]], items2 = [[2,1],[3,2],[1,3]]
Output: [[1,4],[2,4],[3,4]]
Explanation: 
The item with value = 1 occurs in items1 with weight = 1 and in items2 with weight = 3, total weight = 1 + 3 = 4.
The item with value = 2 occurs in items1 with weight = 3 and in items2 with weight = 1, total weight = 3 + 1 = 4.
The item with value = 3 occurs in items1 with weight = 2 and in items2 with weight = 2, total weight = 2 + 2 = 4.
Therefore, we return [[1,4],[2,4],[3,4]].
Example 3:

Input: items1 = [[1,3],[2,2]], items2 = [[7,1],[2,2],[1,4]]
Output: [[1,7],[2,4],[7,1]]
Explanation:
The item with value = 1 occurs in items1 with weight = 3 and in items2 with weight = 4, total weight = 3 + 4 = 7. 
The item with value = 2 occurs in items1 with weight = 2 and in items2 with weight = 2, total weight = 2 + 2 = 4. 
The item with value = 7 occurs in items2 with weight = 1, total weight = 1.
Therefore, we return [[1,7],[2,4],[7,1]].
Constraints:

1 <= items1.length, items2.length <= 1000
items1[i].length == items2[i].length == 2
1 <= valuei, weighti <= 1000
Each valuei in items1 is unique.
Each valuei in items2 is unique.

思路一:用 TreeSet 存储 key,用 map 存储二维数组,最后遍历 TreeSet。看了一下评论区,发现直接用 TreeMap 就可以实现存储有序的值,可以省去 TreeSet

public List<List<Integer>> mergeSimilarItems(int[][] items1, int[][] items2) {
    List<List<Integer>> result = new ArrayList<>();

    Set<Integer> keys = new TreeSet<>();
    Map<Integer, Integer> map = new HashMap<>();
    for (int i = 0; i < items1.length; i++) {
        keys.add(items1[i][0]);
        int finalI = i;
        map.compute(items1[i][0], (k, v) -> v == null ? items1[finalI][1] : v + items1[finalI][1]);
    }
    for (int i = 0; i < items2.length; i++) {
        int finalI = i;
        keys.add(items2[i][0]);
        map.compute(items2[i][0], (k, v) -> v == null ? items2[finalI][1] : v + items2[finalI][1]);
    }

    for (Integer key : keys) {
        List<Integer> list = new ArrayList<>();
        list.add(key);
        list.add(map.get(key));
        result.add(list);
    }

    return result;
}

标签:weight,value,occurs,items2,item,items1,easy,2363,leetcode
From: https://www.cnblogs.com/iyiluo/p/16867224.html

相关文章

  • leetcode-1287-easy
    ElementAppearingMoreThan25%InSortedArrayGivenanintegerarraysortedinnon-decreasingorder,thereisexactlyoneintegerinthearraythatoccursmo......
  • LeetCode125. 验证回文串
    验证回文串Day:2022-11-7link:https://leetcode.cn/problems/valid-palindromequestion:如果在将所有大写字符转换为小写字符、并移除所有非字母数字字符之后,短语正......
  • LeetCode40. 组合总和 II
    题意给一个数组和target,找出数组中所有和为target的组合方法DFS代码classSolution{private:vector<vector<int>>res;vector<int>tmp;public:......
  • 操作步骤:EasyCVR平台国标GB设备如何正确配置告警预案?
    EasyCVR的告警预案是指,在告警配置中,用户可以根据告警类型、告警级别、告警方式、告警事件类型等信息,来具体分类获取告警信息。在上期的文章中,我们介绍了关于EasyCVR平台告警......
  • AI云边端EasyCVR平台支持国标级联时上传设备的自定义经纬度
    EasyCVR平台支持设备上传经纬度,并能在电子地图上显示设备的地理位置。设备可以通过4G、5G等网络实时向平台传输位置信息,结合GIS电子地图,能实现视频监控的空间化、立体化布......
  • leetcode(34)优先队列系列题目
    218.天际线问题用SortedList存边界,每次删除或加入边界判断最高点是否变化classSolution:defgetSkyline(self,buildings:List[List[int]])->List[List[int]]:......
  • EasyPoi大数据导入导出百万级实例
    EasyPoi介绍:利用注解的方式简化了Excel、Word、PDF等格式的导入导出,而且是百万级数据的导入导出。EasyPoi官方网址:EasyPoi教程_V1.0(mydoc.io)。下面我写了一个测试用例......
  • 用Rust刷leetcode第八题
    ProblemImplement ​​atoi​​​ which convertsastringtoaninteger.Thefunctionfirstdiscardsasmanywhitespacecharactersasnecessaryuntilthefirst......
  • [leetcode每日一题]11.7
    816. 模糊坐标我们有一些二维坐标,如 ​​"(1,3)"​​ 或 ​​"(2,0.5)"​​,然后我们移除所有逗号,小数点和空格,得到一个字符串​​S​​。返回所有可能的原始字符串到......
  • [oeasy]python0011_ 字符序号_ordinal_ord
    ​ 序号(ordinal)回忆上次内容​helloworld​​不是从来就有的来自于​​unix​​和​​c​​虽然我们今天有各种先进的学习手段最早的高级语言学习是从最早......