Skip to content

Commit df800e3

Browse files
committed
minimum moves to equal array elements ii
1 parent 80f2bdf commit df800e3

File tree

3 files changed

+64
-35
lines changed

3 files changed

+64
-35
lines changed

.idea/workspace.xml

Lines changed: 34 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,4 @@ codes may not be optimized
406406
### Day 19 - Medium
407407

408408
1. [Path Sum II](https://leetcode.com/problems/path-sum-ii/description/)
409+
1. [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/description/)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.github.chen0040.leetcode.day19.medium;
2+
3+
4+
import java.util.Arrays;
5+
6+
7+
/**
8+
* Created by xschen on 14/8/2017.
9+
*
10+
* link: https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/description/
11+
*/
12+
public class MinimumMovesToEqualArrayElementsII {
13+
public class Solution {
14+
public int minMoves2(int[] nums) {
15+
int sum = 0;
16+
17+
Arrays.sort(nums);
18+
int mid = nums.length / 2;
19+
int mode = nums[mid];
20+
21+
//int avg =sum / nums.length;
22+
int moves = 0;
23+
for(Integer i : nums) {
24+
moves += Math.abs(mode - i);
25+
}
26+
return moves;
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)