Simple java solution beats 97.10% of submissions


  • 0
    V
    public class Solution {
        public int minMoves(int[] a) {
            
    	    int min = Integer.MAX_VALUE;
            int minIndex = 0;
            for(int i = 0 ; i < a.length ; i ++){
                if(a[i] < min) {
                    min = a[i];
                    minIndex = i;
                }
            }
            int sum = 0 ;
            for(int i = 0 ; i < a.length ; i ++) {
                if(i == minIndex) {
                    continue ; 
                }   
                sum = sum + (a[i] - min) ;
            }
            return sum;
        }
        }
    

Log in to reply
 

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