leetcode

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

0561.cpp (474B)


      1 class Solution {
      2     const static int length = 20001;
      3 
      4   public:
      5     int arrayPairSum(vector<int> &nums) {
      6         int arr[length] = {0};
      7 
      8         for (int i : nums)
      9             arr[i + 10000]++;
     10 
     11         int res = 0;
     12         int sub = 0;
     13         for (int i = 0; i < length; i++) {
     14             if (!arr[i]) continue;
     15 
     16             arr[i] -= sub;
     17             res += (arr[i] / 2 + arr[i] % 2) * (i - 10000);
     18             sub = arr[i] % 2;
     19         }
     20         return res;
     21     }
     22 };