0945.cpp (517B)
1 class Solution { 2 public: 3 int minIncrementForUnique(const vector<int> &nums) const { 4 static int count[100001]; 5 memset(count, 0x00, sizeof(count)); 6 7 for (const int n : nums) 8 count[n]++; 9 10 int res = 0, carry = 0; 11 for (int i = 0; i < 100001; i++) { 12 if (count[i] >= 1) 13 carry += count[i] - 1; 14 else if (carry) 15 carry--; 16 res += carry; 17 } 18 19 return res + carry * (carry - 1) / 2; 20 } 21 };