[LC 453]. Minimum Moves to Equal Array Elements

再写一个脑筋急转弯的题吧

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n – 1 elements by 1. Example: Input: [1,2,3], Output:3. [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]

给n-1个element-1就相当于给最大值+1…
所以move总数相当于把所有数字减到最小值需要的总步数…

res = sum – min * n

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s