forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 8
/
_462.java
59 lines (46 loc) · 1.52 KB
/
_462.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.fishercoder.solutions;
import java.util.Arrays;
/**462. Minimum Moves to Equal Array Elements II
* Given a non-empty integer array,
* find the minimum number of moves required to make all array elements equal,
* where a move is incrementing a selected element by 1 or decrementing a selected element by 1.
You may assume the array's length is at most 10,000.
Example:
Input:
[1,2,3]
Output:
2
Explanation:
Only two moves are needed (remember each move increments or decrements one element):
[1,2,3] => [2,2,3] => [2,2,2]*/
public class _462 {
public static int minMoves2(int[] nums) {
/**sort this array, find the median of this array as the pivot*/
Arrays.sort(nums);
int result = 0;
int result1 = 0;
if (nums.length % 2 != 0) {
int median = nums[nums.length / 2];
for (int n : nums) {
result += Math.abs(n - median);
}
return result;
} else {
int median1 = nums[nums.length / 2];
for (int n : nums) {
result1 += Math.abs(n - median1);
}
int result2 = 0;
int median2 = nums[nums.length / 2 - 1];
for (int n : nums) {
result2 += Math.abs(n - median2);
}
result1 = Math.min(result1, result2);
return result1;
}
}
public static void main(String... args) {
int[] nums = new int[]{1, 2, 3};
System.out.println(minMoves2(nums));
}
}