Skip to content

Commit c1cdfe9

Browse files
authored
Create 945. Minimum Increment to Make Array Unique (#503)
2 parents af524cd + fa3241f commit c1cdfe9

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int minIncrementForUnique(std::vector<int>& nums) {
4+
if (nums.empty()) return 0;
5+
int moves = 0,maxVal = 0;
6+
for(auto c:nums)maxVal=max(maxVal,c);
7+
int maxLength = nums.size() + maxVal;
8+
vector<int> count(maxLength, 0);
9+
for (int c : nums) count[c]++;
10+
for (int i = 0; i < maxLength; ++i) {
11+
if (count[i] > 1) {
12+
int tmp = count[i] - 1;
13+
count[i + 1] += tmp;
14+
moves += tmp;
15+
}
16+
}
17+
return moves;
18+
}
19+
};

0 commit comments

Comments
 (0)