-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2363-MergeSimilarItems.go
More file actions
116 lines (104 loc) · 5.26 KB
/
2363-MergeSimilarItems.go
File metadata and controls
116 lines (104 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
// 2363. 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:
// 1. items[i] = [valuei, weighti] where valuei represents the value and weighti represents the weight of the ith item.
// 2. 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.
import "fmt"
import "sort"
func mergeSimilarItems(items1 [][]int, items2 [][]int) [][]int {
mp := make(map[int]int)
for _, v := range items1 {
mp[v[0]] += v[1]
}
for _, v := range items2 {
mp[v[0]] += v[1]
}
res := make([][]int, 0, len(mp))
for k, v := range mp {
res = append(res, []int{ k, v })
}
sort.Slice(res, func(i, j int) bool {
return res[i][0] < res[j][0]
})
return res
}
func mergeSimilarItems1(items1 [][]int, items2 [][]int) [][]int {
mp := make([]int, 1001)
for _, v := range items1 {
mp[v[0]] += v[1]
}
for _, v := range items2 {
mp[v[0]] += v[1]
}
res := [][]int{}
for k, v := range mp {
if v == 0 { continue }
res = append(res, []int{ k, v })
}
return res
}
func main() {
// 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]].
fmt.Println(mergeSimilarItems([][]int{{1,1},{4,5},{3,8}}, [][]int{{3,1},{1,5}})) // [[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]].
fmt.Println(mergeSimilarItems([][]int{{1,1},{3,2},{2,3}}, [][]int{{2,1},{3,2},{1,3}})) // [[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]].
fmt.Println(mergeSimilarItems([][]int{{1,3},{2,2}}, [][]int{{7,1},{2,2},{1,4}})) // [[1,7],[2,4],[7,1]]
fmt.Println(mergeSimilarItems1([][]int{{1,1},{4,5},{3,8}}, [][]int{{3,1},{1,5}})) // [[1,6],[3,9],[4,5]]
fmt.Println(mergeSimilarItems1([][]int{{1,1},{3,2},{2,3}}, [][]int{{2,1},{3,2},{1,3}})) // [[1,4],[2,4],[3,4]]
fmt.Println(mergeSimilarItems1([][]int{{1,3},{2,2}}, [][]int{{7,1},{2,2},{1,4}})) // [[1,7],[2,4],[7,1]]
}