Given a non-empty integer array, find the minimum number of moves required to make all array elements equal, where a move is incrementing a selected element by 1 or decrementing a selected element by 1.
You may assume the array ' s length are at 10,000.
Example:
Input:[1,2,3]output:2explanation:only moves is needed (remember each move increments or decrements one element): [1, 2, 3] = [2,2,3] [ 2,2,2]
Test instructions
Use the smallest operation to make all the values in the array equal.
Ideas:
Sorting, finding out the absolute value of all the values in the array and the interpolated values of the intermediate bits of the index
This question is written in Python, this time to see the efficiency of Python,
1 classsolution (object):2 defminMoves2 (Self, nums):3 """4 : Type Nums:list[int]5 : Rtype:int6 """7 Nums.sort ()8Mid=nums[len (nums)/2]9 returnSUM (ABS (Mid-num) forNuminchNums
"Leetcode" 462. Minimum Moves to Equal Array Elements II