Skip to content

Commit 98af94d

Browse files
committed
Time: 302 ms (13.38%), Space: 101.7 MB (10.24%) - LeetHub
1 parent 6c18678 commit 98af94d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int minIncrementForUnique(vector<int>& nums) {
4+
map<int, int> mp;
5+
for (auto& i : nums) {
6+
mp[i]++;
7+
}
8+
long long sum = -1;
9+
long long ans = 0;
10+
for (auto& i : mp) {
11+
if (sum < i.first) {
12+
long long x = i.second - 1;
13+
ans += x * (x + 1) / 2;
14+
sum = i.first + x;
15+
} else {
16+
long long x = i.second - 1;
17+
ans += (sum - i.first + 1) * i.second;
18+
ans += x * (x + 1) / 2;
19+
sum += i.second;
20+
}
21+
22+
}
23+
return ans;
24+
}
25+
};

0 commit comments

Comments
 (0)