An Increment Problem
Given a stream of numbers A. On arrival of each number, you need to increase its first occurence by 1 and include this in the stream.
Return the final stream of numbers.
ip:
A = [1, 1]
op:
[2, 1]
vector<int> Solution::solve(vector<int> &a) {
unordered_map<int,int> m;
for(int i=0; i<a.size(); i++)
{
if(m.count(a[i]))
{
a[m[a[i]]]++;
m[a[i]+1] = m[a[i]];
}
m[a[i]] = i;
}
return a;
}
Comments
Post a Comment