C++ with sorted median


  • 0
    O
    class Solution {
    public:
        int minMoves2(vector<int>& nums) 
        {
            int totalMoves = 0;
            int median = GetSortedMedian(nums);
    
            for(int i = 0 ; i < nums.size() ; i++ )
                totalMoves += abs(median - nums[i]);
            
            return totalMoves;
        }
        
        int GetSortedMedian(vector<int>& nums)
        {
            sort(nums.begin(), nums.end());
            
            return nums[nums.size() / 2];
        }
        
    };
    

Log in to reply
 

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