leetcode#453 the minimum number of moves to make an array element equal

Source: Internet
Author: User

Given an array of non-empty integers of length n , find the minimum number of moves that allow all elements of the array to be equal. Each move can increase the n -1 elements by 1.

Example:

input: [1,2,3] Output: 3 Explanation: Only 3 moves are required (note that each move increases the value of two elements): [1,2,3]  [2,3,3]  = =  [3,4,3]  =  [4,4,4]

Self-reflection:
Observe the law, the number of moves is the maximum value in the array minus the minimum value, and then the new array to continue to use the maximum value minus the minimum value until the difference is 0, each time the difference is the number of moves
The code is implemented as follows

1 Importjava.io.IOException;2 ImportJava.util.Scanner;3 4 5  Public classMainClass {6      Public Static voidMain (string[] args)throwsioexception{7Scanner input =NewScanner (system.in);8System.out.println ("Please enter an array, separated by commas between elements:"));9         int[] nums=StringToArray (Input.nextline ());TenSolution getmoves=Newsolution (); One         intmoves=getmoves.minmoves (nums); ASYSTEM.OUT.PRINTLN ("Minimum number of moves:"); - System.out.println (moves); -     } the      Public Static int[] StringToArray (String str) { -String[] strarr= str.split (","); -         int[] arr=New int[strarr.length]; -          for(inti=0;i<strarr.length;i++){ +arr[i]=Integer.parseint (Strarr[i].trim ()); -         } +         returnarr; A     } at } -  - classSolution { -      Public intMinmoves (int[] nums) { -         intMinmoves=move (nums,0); -          in         returnminmoves; -     } to      +      Public int[] Submaxmin (int[] nums) { -         intMax=0,min=0; the          for(inti=1;i<nums.length;i++){ *             if(nums[max]<Nums[i]) { $max=i;Panax Notoginseng             } -             if(nums[min]>Nums[i]) { themin=i; +             } A         } the         int[] maxandmin={max,min}; +         returnmaxandmin; -     } $      $      Public intMoveint[] Nums,intsummove) { -         int[] maxandmin=submaxmin (nums); -         intMoves=nums[maxandmin[0]]-nums[maxandmin[1]]; the         if(moves!=0){ -summove+=moves;Wuyi              for(inti=0;i<nums.length;i++){ the                 if(i!=maxandmin[0]){ -nums[i]+=moves; Wu                 } -             } About             returnMove (nums,summove); $}Else{ -             returnSummove; -         } -     } A}

This implementation after Leetcode submitted, because time-out, was rejected, although in Myeclipes can be run quickly, but online debugging shows that the time is very high, [A. "] takes time around 120ms.
Baidu found: The core of this algorithm is the number of moves is the array of other elements minus the minimum value and so ....

1 classSolution {2      Public intMinmoves (int[] nums) {3         intSum=0,moves,min=nums[0];4          for(inti=0;i<nums.length;i++){5sum+=Nums[i];6             if(nums[i]<min) {7min=Nums[i];8             }9         }Tenmoves=sum-min*nums.length; One         returnmoves; A     } -}

How many slap do you smoke?!!



leetcode#453 the minimum number of moves to make an array element equal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.