diff --git a/945. Minimum Increment to Make Array Unique b/945. Minimum Increment to Make Array Unique new file mode 100644 index 0000000..62e2124 --- /dev/null +++ b/945. Minimum Increment to Make Array Unique @@ -0,0 +1,19 @@ +class Solution { +public: + int minIncrementForUnique(std::vector& nums) { + if (nums.empty()) return 0; + int moves = 0,maxVal = 0; + for(auto c:nums)maxVal=max(maxVal,c); + int maxLength = nums.size() + maxVal; + vector count(maxLength, 0); + for (int c : nums) count[c]++; + for (int i = 0; i < maxLength; ++i) { + if (count[i] > 1) { + int tmp = count[i] - 1; + count[i + 1] += tmp; + moves += tmp; + } + } + return moves; + } +};