LeetCode 453. Minimum Moves to Equal Array Elements
Posted 几米空间
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 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 Explanation: Only three moves are needed (remember each move increments two elements): [1,2,3] => [2,3,3] => [3,4,3] => [4,4,4]
题目标签:Math
* for example: [1,2,3] -> [2,3,3] -> [3,3,4] -> [4,4,4]
* now, think in this way: [1,2,3] -> [1,2,2] -> [1,1,2] -> [1,1,1]
Java Solution:
Runtime beats 77.57%
完成日期:06/17/2017
关键词:math
关键点:逆向思考
1 class Solution 2 { 3 public int minMoves(int[] nums) 4 { 5 int moves = 0; 6 int min = Integer.MAX_VALUE; 7 8 // find the min value 9 for(int num : nums) 10 min = Math.min(min, num); 11 12 13 // iterate array and calculate the sum of difference between each number and min 14 for(int num : nums) 15 moves += (num - min); 16 17 return moves; 18 } 19 }
参考资料:http://www.cnblogs.com/grandyang/p/6053827.html
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
以上是关于LeetCode 453. Minimum Moves to Equal Array Elements的主要内容,如果未能解决你的问题,请参考以下文章
453. Minimum Moves to Equal Array Elements(LeetCode)
[leetcode-453-Minimum Moves to Equal Array Elements]
LeetCode 453. Minimum Moves to Equal Array Elements
[LeetCode&Python] Problem 453. Minimum Moves to Equal Array Elements
LeetCode 453. Minimum Moves to Equal Array Elements C#
(Java) LeetCode 453. Minimum Moves to Equal Array Elements —— 最小移动次数使数组元素相等