Shawngbk / Leecode

Questions of Leecode
0 stars 0 forks source link

453. Minimum Moves to Equal Array Elements #140

Open Shawngbk opened 8 years ago

Shawngbk commented 8 years ago

The key idea is increasing n - 1 elements by 1 has the same effect of decreasing 1 element by 1 So we first find the minimum value of the array and count the steps of decreasing every element to the minimum.

public class Solution { public int minMoves(int[] nums) { int min = Integer.MAX_VALUE; for(int x : nums) { min = Math.min(min, x); } int count = 0; for(int x : nums) { count = count + (x - min); } return count; } }

Shawngbk commented 8 years ago

Coursera Indeed