Skip to content

Commit 4a0ecd2

Browse files
refactor 462
1 parent 31cc778 commit 4a0ecd2

File tree

2 files changed

+53
-32
lines changed

2 files changed

+53
-32
lines changed

‎src/main/java/com/fishercoder/solutions/_462.java

+31-32
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import java.util.Arrays;
44

5-
/**462. Minimum Moves to Equal Array Elements II
5+
/**
6+
* 462. Minimum Moves to Equal Array Elements II
7+
*
68
* Given a non-empty integer array,
79
* find the minimum number of moves required to make all array elements equal,
810
* where a move is incrementing a selected element by 1 or decrementing a selected element by 1.
9-
10-
You may assume the array's length is at most 10,000.
11+
*
12+
* You may assume the array's length is at most 10,000.
1113
1214
Example:
1315
@@ -20,40 +22,37 @@
2022
Explanation:
2123
Only two moves are needed (remember each move increments or decrements one element):
2224
23-
[1,2,3] => [2,2,3] => [2,2,2]*/
25+
[1,2,3] => [2,2,3] => [2,2,2]
26+
*/
2427

2528
public class _462 {
2629

27-
public static int minMoves2(int[] nums) {
28-
/**sort this array, find the median of this array as the pivot*/
29-
Arrays.sort(nums);
30-
int result = 0;
31-
int result1 = 0;
32-
if (nums.length % 2 != 0) {
33-
int median = nums[nums.length / 2];
34-
for (int n : nums) {
35-
result += Math.abs(n - median);
36-
}
37-
return result;
38-
} else {
39-
int median1 = nums[nums.length / 2];
40-
for (int n : nums) {
41-
result1 += Math.abs(n - median1);
42-
}
43-
int result2 = 0;
44-
int median2 = nums[nums.length / 2 - 1];
45-
for (int n : nums) {
46-
result2 += Math.abs(n - median2);
30+
public static class Solution1 {
31+
public int minMoves2(int[] nums) {
32+
/**sort this array, find the median of this array as the pivot*/
33+
Arrays.sort(nums);
34+
int result = 0;
35+
int result1 = 0;
36+
if (nums.length % 2 != 0) {
37+
int median = nums[nums.length / 2];
38+
for (int n : nums) {
39+
result += Math.abs(n - median);
40+
}
41+
return result;
42+
} else {
43+
int median1 = nums[nums.length / 2];
44+
for (int n : nums) {
45+
result1 += Math.abs(n - median1);
46+
}
47+
int result2 = 0;
48+
int median2 = nums[nums.length / 2 - 1];
49+
for (int n : nums) {
50+
result2 += Math.abs(n - median2);
51+
}
52+
result1 = Math.min(result1, result2);
53+
return result1;
4754
}
48-
result1 = Math.min(result1, result2);
49-
return result1;
5055
}
51-
52-
}
53-
54-
public static void main(String... args) {
55-
int[] nums = new int[]{1, 2, 3};
56-
System.out.println(minMoves2(nums));
5756
}
5857

5958
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._462;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _462Test {
10+
private static _462.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _462.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(2, solution1.minMoves2(new int[]{1, 2, 3}));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)