From 9796f7635eb2762e6d35e08e3bbacfc33a87b75a Mon Sep 17 00:00:00 2001 From: chenzhengwei Date: Mon, 27 Feb 2017 15:31:04 +0800 Subject: [PATCH] a453 --- README.md | 2 ++ Solution/451-500/453.md | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 Solution/451-500/453.md diff --git a/README.md b/README.md index 5d70eb2..401a8fe 100644 --- a/README.md +++ b/README.md @@ -132,3 +132,5 @@ leetcode 题解 [416. Partition Equal Subset Sum](Solution/401-450/416.md) +[453. Minimum Moves to Equal Array Elements](Solution/451-500/453.md) + diff --git a/Solution/451-500/453.md b/Solution/451-500/453.md new file mode 100644 index 0000000..d751c0a --- /dev/null +++ b/Solution/451-500/453.md @@ -0,0 +1,24 @@ +#453. Minimum Moves to Equal Array Elements +[题目链接](https://leetcode.com/problems/minimum-moves-to-equal-array-elements/?tab=Description) +```java +public class Solution { + /* + 一共有n个数字,每次有n-1个数字加一, + 可以转换为每次有一个数字减一, + 这样就可以转换为每个数见到最小数字的总和 + */ + public int minMoves(int[] nums) { + int min = Integer.MAX_VALUE; + for (int num : nums) { + if (num < min) { + min = num; + } + } + int moves = 0; + for (int num : nums) { + moves += (num - min); + } + return moves; + } +} +``` \ No newline at end of file