We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent cfd2ec5 commit 2ecabd2Copy full SHA for 2ecabd2
0945-minimum-increment-to-make-array-unique/0945-minimum-increment-to-make-array-unique.cpp
@@ -1,25 +1,20 @@
1
class Solution {
2
public:
3
int minIncrementForUnique(vector<int>& nums) {
4
- map<int, int> mp;
+ vector<int> f(1e5 + 5);
5
for (auto& i : nums) {
6
- mp[i]++;
+ f[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
17
- ans += (sum - i.first + 1) * i.second;
18
19
- sum += i.second;
+ for (int i = 0; i <= 1e5; i++) {
+ if (f[i] > 1) {
+ ans += f[i] - 1;
+ f[i + 1] += f[i] - 1;
20
21
-
22
+ long long x = f[1e5 + 1];
+ x--;
+ ans += x * (x + 1) / 2;
23
return ans;
24
25
};
0 commit comments