forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_462.java
35 lines (31 loc) · 1.03 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
package com.fishercoder.solutions;
import java.util.Arrays;
public class _462 {
public static class Solution1 {
public 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;
}
}
}
}