Skip to content

Commit

Permalink
added Merge Similar Items problem
Browse files Browse the repository at this point in the history
  • Loading branch information
mukul96 committed Nov 8, 2022
1 parent 1282885 commit 55e1693
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Ordered Set/Merge Similar Items.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution
{
public:
vector<vector<int> > mergeSimilarItems(vector<vector<int> > &items1, vector<vector<int> > &items2)
{
map<int, int> mp;
for (int i = 0; i < items1.size(); i++)
{
mp[items1[i][0]] += items1[i][1];
}
for (int i = 0; i < items2.size(); i++)
{
mp[items2[i][0]] += items2[i][1];
}

vector<vector<int> > res;
for (auto it = mp.begin(); it != mp.end(); ++it)
{
vector<int> temp;
temp.push_back(it->first);
temp.push_back(it->second);
res.push_back(temp);
}
return res;
}
};

0 comments on commit 55e1693

Please sign in to comment.