[InterviewBit] An Increment Problem

An Increment Problem

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
vector<int> Solution::solve(vector<int> &A) {
unordered_map<int, set<int>> mp;
for(int i = 0; i < A.size(); i++) {
if(mp.count(A[i])) {
auto b = *mp[A[i]].begin();
mp[A[i]].erase(b);
A[b] += 1;
mp[A[b]].insert(b);
}
mp[A[i]].insert(i);
}
return A;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/02/PS/interviewbit/an-increment-problem/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.