-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2570.cpp
More file actions
58 lines (56 loc) · 1.66 KB
/
2570.cpp
File metadata and controls
58 lines (56 loc) · 1.66 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
class Solution {
public:
vector<vector<int>> mergeArrays(vector<vector<int>>& nums1, vector<vector<int>>& nums2) {
map<int, int> mp;
for (auto& num : nums1) {
mp[num[0]] += num[1];
}
for (auto& num : nums2) {
mp[num[0]] += num[1];
}
vector<vector<int>> res;
for (auto& [id, value] : mp) {
res.push_back({id, value});
}
return res;
}
};
class Solution {
public:
vector<vector<int>> mergeArrays(vector<vector<int>>& nums1, vector<vector<int>>& nums2) {
int index1 = 0;
int index2 = 0;
vector<vector<int>> res;
int m = nums1.size();
int n = nums2.size();
while (index1 < m || index2 < n) {
if (index1 < m && index2 < n) {
int id1 = nums1[index1][0];
int id2 = nums2[index2][0];
if (id1 == id2) {
int sum = nums1[index1][1] + nums2[index2][1];
res.push_back({id1, sum});
index1++;
index2++;
}
else if (id1 > id2) {
res.push_back({id2, nums2[index2][1]});
index2++;
}
else if (id1 < id2) {
res.push_back({id1, nums1[index1][1]});
index1++;
}
}
else if (index1 < m) {
res.push_back(nums1[index1]);
index1++;
}
else if (index2 < n) {
res.push_back(nums2[index2]);
index2++;
}
}
return res;
}
};