We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 6c18678 commit 98af94dCopy full SHA for 98af94d
0945-minimum-increment-to-make-array-unique/0945-minimum-increment-to-make-array-unique.cpp
@@ -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
17
+ ans += (sum - i.first + 1) * i.second;
18
19
+ sum += i.second;
20
21
+
22
23
+ return ans;
24
25
+};
0 commit comments