C++ Greedy Solution


  • 0
    Y
    class Solution {
    public:
        int arrayPairSum(vector<int>& nums) {
            int sum = 0;
            if(nums.size() == 0)
                return sum;
            sort(nums.begin(), nums.end());
            for(int i = 0; i < nums.size(); i += 2)
                sum += nums[i];
            return sum;
        }
    };
    

Log in to reply
 

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