leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0945.cpp (517B)


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