C++ solution with quick select


  • 0
    S

    class Solution {
    public:
    int minMoves2(vector<int>& nums) {
    int mid = nums.size()/2;
    nth_element(nums.begin(), nums.begin()+mid, nums.end());

        int x= nums[mid], res=0;
        for (auto n:nums) 
            res += abs(n-x);
    
        return res;
    }
    

    };

    //use quick select by nth_element().


Log in to reply
 

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