Skip to content

Commit 2ecabd2

Browse files
committed
Time: 81 ms (97.44%), Space: 96.4 MB (11.81%) - LeetHub
1 parent cfd2ec5 commit 2ecabd2

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
class Solution {
22
public:
33
int minIncrementForUnique(vector<int>& nums) {
4-
map<int, int> mp;
4+
vector<int> f(1e5 + 5);
55
for (auto& i : nums) {
6-
mp[i]++;
6+
f[i]++;
77
}
8-
long long sum = -1;
98
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;
9+
for (int i = 0; i <= 1e5; i++) {
10+
if (f[i] > 1) {
11+
ans += f[i] - 1;
12+
f[i + 1] += f[i] - 1;
2013
}
21-
2214
}
15+
long long x = f[1e5 + 1];
16+
x--;
17+
ans += x * (x + 1) / 2;
2318
return ans;
2419
}
2520
};

0 commit comments

Comments
 (0)