|
| 1 | +// https://leetcode.com/problems/merge-similar-items |
| 2 | +// |
| 3 | +// You are given two 2D integer arrays, `items1` and `items2`, representing two sets of items. Each array `items` has the following properties: |
| 4 | +// |
| 5 | +// * `items[i] = [value<sub>i</sub>, weight<sub>i</sub>]` where `value<sub>i</sub>` represents the **value** and `weight<sub>i</sub>` represents the **weight** of the `i<sup>th</sup>` item. |
| 6 | +// * The value of each item in `items` is **unique**. |
| 7 | +// |
| 8 | +// Return _a 2D integer array_ `ret` _where_ `ret[i] = [value<sub>i</sub>, weight<sub>i</sub>]`_,_ _with_ `weight<sub>i</sub>` _being the **sum of weights** of all items with value_ `value<sub>i</sub>`. |
| 9 | +// |
| 10 | +// **Note:** `ret` should be returned in **ascending** order by value. |
| 11 | +// |
| 12 | +// **Example 1:** |
| 13 | +// |
| 14 | +// ``` |
| 15 | +// **Input:** items1 = [[1,1],[4,5],[3,8]], items2 = [[3,1],[1,5]] |
| 16 | +// **Output:** [[1,6],[3,9],[4,5]] |
| 17 | +// **Explanation:** |
| 18 | +// The item with value = 1 occurs in items1 with weight = 1 and in items2 with weight = 5, total weight = 1 + 5 = 6. |
| 19 | +// The item with value = 3 occurs in items1 with weight = 8 and in items2 with weight = 1, total weight = 8 + 1 = 9. |
| 20 | +// The item with value = 4 occurs in items1 with weight = 5, total weight = 5\. |
| 21 | +// Therefore, we return [[1,6],[3,9],[4,5]]. |
| 22 | +// ``` |
| 23 | +// |
| 24 | +// **Example 2:** |
| 25 | +// |
| 26 | +// ``` |
| 27 | +// **Input:** items1 = [[1,1],[3,2],[2,3]], items2 = [[2,1],[3,2],[1,3]] |
| 28 | +// **Output:** [[1,4],[2,4],[3,4]] |
| 29 | +// **Explanation:** |
| 30 | +// The item with value = 1 occurs in items1 with weight = 1 and in items2 with weight = 3, total weight = 1 + 3 = 4. |
| 31 | +// The item with value = 2 occurs in items1 with weight = 3 and in items2 with weight = 1, total weight = 3 + 1 = 4. |
| 32 | +// The item with value = 3 occurs in items1 with weight = 2 and in items2 with weight = 2, total weight = 2 + 2 = 4. |
| 33 | +// Therefore, we return [[1,4],[2,4],[3,4]].``` |
| 34 | +// |
| 35 | +// **Example 3:** |
| 36 | +// |
| 37 | +// ``` |
| 38 | +// **Input:** items1 = [[1,3],[2,2]], items2 = [[7,1],[2,2],[1,4]] |
| 39 | +// **Output:** [[1,7],[2,4],[7,1]] |
| 40 | +// **Explanation:** The item with value = 1 occurs in items1 with weight = 3 and in items2 with weight = 4, total weight = 3 + 4 = 7\. |
| 41 | +// The item with value = 2 occurs in items1 with weight = 2 and in items2 with weight = 2, total weight = 2 + 2 = 4\. |
| 42 | +// The item with value = 7 occurs in items2 with weight = 1, total weight = 1. |
| 43 | +// Therefore, we return [[1,7],[2,4],[7,1]]. |
| 44 | +// ``` |
| 45 | +// |
| 46 | +// **Constraints:** |
| 47 | +// |
| 48 | +// * `1 <= items1.length, items2.length <= 1000` |
| 49 | +// * `items1[i].length == items2[i].length == 2` |
| 50 | +// * `1 <= value<sub>i</sub>, weight<sub>i</sub> <= 1000` |
| 51 | +// * Each `value<sub>i</sub>` in `items1` is **unique**. |
| 52 | +// * Each `value<sub>i</sub>` in `items2` is **unique**. |
| 53 | + |
| 54 | +pub fn merge_similar_items(items1: Vec<Vec<i32>>, items2: Vec<Vec<i32>>) -> Vec<Vec<i32>> { |
| 55 | + let mut result_hash = std::collections::HashMap::new(); |
| 56 | + for item in items1 { |
| 57 | + *result_hash.entry(item[0]).or_default() += item[1] |
| 58 | + } |
| 59 | + for item in items2 { |
| 60 | + *result_hash.entry(item[0]).or_default() += item[1] |
| 61 | + } |
| 62 | + let mut res = vec![]; |
| 63 | + for (key, value) in result_hash { |
| 64 | + res.push(vec![key, value]); |
| 65 | + } |
| 66 | + res.sort_by(|a, b| a[0].cmp(&b[0])); |
| 67 | + return res; |
| 68 | +} |
| 69 | + |
| 70 | +#[test] |
| 71 | +pub fn t1() { |
| 72 | + let items1 = vec![vec![1, 1], vec![4, 5], vec![3, 8]]; |
| 73 | + let items2 = vec![vec![3, 1], vec![1, 5]]; |
| 74 | + let ret = vec![vec![1, 6], vec![3, 9], vec![4, 5]]; |
| 75 | + assert_eq!(merge_similar_items(items1, items2), ret); |
| 76 | +} |
0 commit comments