C++ Bucket Sort 7 lines 59 ms


  • 0
    V
    int arrayPairSum(vector<int>& nums) {
        int buckets[20001] = {}, res = 0, minv = 10000, maxv = -10000;
        auto take = true;
        for (auto n : nums) ++buckets[n + 10000], minv = min(minv, n), maxv = max(maxv, n);
        for (; minv <= maxv; ++minv) {
            res += minv * (buckets[minv + 10000] / 2);
            if (buckets[minv + 10000] % 2) res += take ? minv : 0, take = !take;
        }
        return res;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.