Skip to content

Commit 1314e8d

Browse files
refactor 229
1 parent a423b86 commit 1314e8d

File tree

2 files changed

+31
-44
lines changed

2 files changed

+31
-44
lines changed

src/main/java/com/fishercoder/solutions/_229.java

+16-35
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package com.fishercoder.solutions;
22

33
import java.util.ArrayList;
4-
import java.util.HashMap;
54
import java.util.List;
6-
import java.util.Map;
75

86
/**
97
* 229. Majority Element II
108
*
119
* Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.
1210
* The algorithm should run in linear time and in O(1) space.
11+
*
12+
* Example 1:
13+
* Input: [3,2,3]
14+
* Output: [3]
15+
*
16+
* Example 2:
17+
* Input: [1,1,1,3,3,2,2,2]
18+
* Output: [1,2]
1319
1420
Hint:
1521
How many majority elements could it possibly have?
@@ -18,29 +24,9 @@
1824
public class _229 {
1925

2026
public static class Solution1 {
21-
public List<Integer> majorityElement(int[] nums) {
22-
Map<Integer, Integer> counterMap = new HashMap();
23-
for (int i = 0; i < nums.length; i++) {
24-
if (counterMap.containsKey(nums[i])) {
25-
counterMap.put(nums[i], counterMap.get(nums[i]) + 1);
26-
} else {
27-
counterMap.put(nums[i], 1);
28-
}
29-
}
30-
int size = nums.length;
31-
List<Integer> result = new ArrayList<>();
32-
for (Integer i : counterMap.keySet()) {
33-
int threshold = size / 3;
34-
if (counterMap.get(i) > threshold) {
35-
result.add(i);
36-
}
37-
}
38-
return result;
39-
}
40-
}
41-
42-
public static class Solution2 {
43-
/**Moore Voting algorithm*/
27+
/**Moore Voting algorithm:
28+
* This is an extension of Majority Element I, instead of one one majority, there could be a max of two majority elements,
29+
* so we'll just use two counters to do the job.*/
4430
public List<Integer> majorityElement(int[] nums) {
4531
List<Integer> result = new ArrayList<>();
4632
if (nums == null || nums.length == 0) {
@@ -70,20 +56,15 @@ public List<Integer> majorityElement(int[] nums) {
7056
count2 = 0;
7157
for (int num : nums) {
7258
if (num == candidate1) {
73-
count1 += 2;
74-
} else {
75-
count1--;
76-
}
77-
if (num == candidate2) {
78-
count2 += 2;
79-
} else {
80-
count2--;
59+
count1++;
60+
} else if (num == candidate2) {
61+
count2++;
8162
}
8263
}
83-
if (count1 > 0) {
64+
if (count1 > nums.length/3) {
8465
result.add(candidate1);
8566
}
86-
if (count2 > 0) {
67+
if (count2 > nums.length/3) {
8768
result.add(candidate2);
8869
}
8970
return result;

src/test/java/com/fishercoder/_229Test.java

+15-9
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,60 @@
99
import static org.junit.Assert.assertEquals;
1010

1111
public class _229Test {
12-
private static _229.Solution2 solution2;
12+
private static _229.Solution1 solution1;
1313
private static int[] nums;
1414

1515
@BeforeClass
1616
public static void setup() {
17-
solution2 = new _229.Solution2();
17+
solution1 = new _229.Solution1();
1818
}
1919

2020
@Test
2121
public void test1() {
2222
nums = new int[]{1};
23-
assertEquals(Arrays.asList(1), solution2.majorityElement(nums));
23+
assertEquals(Arrays.asList(1), solution1.majorityElement(nums));
2424
}
2525

2626
@Test
2727
public void test2() {
2828
nums = new int[]{1, 2};
29-
assertEquals(Arrays.asList(2, 1), solution2.majorityElement(nums));
29+
assertEquals(Arrays.asList(2, 1), solution1.majorityElement(nums));
3030
}
3131

3232
@Test
3333
public void test3() {
3434
nums = new int[]{2, 2};
35-
assertEquals(Arrays.asList(2), solution2.majorityElement(nums));
35+
assertEquals(Arrays.asList(2), solution1.majorityElement(nums));
3636
}
3737

3838
@Test
3939
public void test4() {
4040
nums = new int[]{1, 2, 3};
41-
assertEquals(Arrays.asList(), solution2.majorityElement(nums));
41+
assertEquals(Arrays.asList(), solution1.majorityElement(nums));
4242
}
4343

4444
@Test
4545
public void test5() {
4646
nums = new int[]{3, 2, 3};
47-
assertEquals(Arrays.asList(3), solution2.majorityElement(nums));
47+
assertEquals(Arrays.asList(3), solution1.majorityElement(nums));
4848
}
4949

5050
@Test
5151
public void test6() {
5252
nums = new int[]{3, 3, 4};
53-
assertEquals(Arrays.asList(3), solution2.majorityElement(nums));
53+
assertEquals(Arrays.asList(3), solution1.majorityElement(nums));
5454
}
5555

5656
@Test
5757
public void test7() {
5858
nums = new int[]{2, 2, 1, 3};
59-
assertEquals(Arrays.asList(2), solution2.majorityElement(nums));
59+
assertEquals(Arrays.asList(2), solution1.majorityElement(nums));
60+
}
61+
62+
@Test
63+
public void test8() {
64+
nums = new int[]{2, 2, 3, 3, 2, 4};
65+
assertEquals(Arrays.asList(2), solution1.majorityElement(nums));
6066
}
6167

6268
}

0 commit comments

Comments
 (0)