-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path029_MajorityElement.java
163 lines (135 loc) · 4.68 KB
/
029_MajorityElement.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Coding Interviews: Questions, Analysis & Solutions
// Harry He
import java.util.Random;
public class MajorityElement {
//================= Solution 1 =================
public static int getMajority_1(int[] numbers){
int length = numbers.length;
int middle = length >> 1;
int start = 0;
int end = length - 1;
int index = partition(numbers, start, end);
while(index != middle){
if(index > middle){
end = index - 1;
index = partition(numbers, start, end);
}
else{
start = index + 1;
index = partition(numbers, start, end);
}
}
int result = numbers[middle];
if(!checkMajorityExistence(numbers, result))
throw new IllegalArgumentException("No majority exisits.");
return result;
}
private static int partition(int[] numbers, int start, int end){
int index = randomInRange(start, end);
swap(numbers, index, end);
int small = start - 1;
for(index = start; index < end; ++index){
if(numbers[index] < numbers[end]){
++small;
if(small != index)
swap(numbers, index, small);
}
}
++small;
swap(numbers, small, end);
return small;
}
private static int randomInRange(int start, int end){
Random random = new Random();
return random.nextInt(end - start + 1) + start;
}
private static void swap(int[] numbers, int index1, int index2){
int temp = numbers[index1];
numbers[index1] = numbers[index2];
numbers[index2] = temp;
}
//================= Solution 2 =================
public static int getMajority_2(int[] numbers){
int result = numbers[0];
int times = 1;
for(int i = 1; i < numbers.length; ++i){
if(times == 0){
result = numbers[i];
times = 1;
}
else if(numbers[i] == result)
times++;
else
times--;
}
if(!checkMajorityExistence(numbers, result))
throw new IllegalArgumentException("No majority exisits.");
return result;
}
//================= Common methods =================
private static boolean checkMajorityExistence(int[] numbers, int number){
int times = 0;
for(int i = 0; i < numbers.length; ++i){
if(numbers[i] == number)
times++;
}
return (times * 2 > numbers.length);
}
//================= Test Code =================
private static void test(String testName, int[] numbers, int expected, boolean invalidArgument){
System.out.print(testName + " begins: ");
try {
int majority = getMajority_1(numbers);
if(!invalidArgument && majority == expected)
System.out.print("Solution1 Passed; ");
else
System.out.print("Solution1 FAILED; ");
}
catch(IllegalArgumentException e){
if(invalidArgument)
System.out.print("Solution1 Passed; ");
else
System.out.print("Solution1 FAILED; ");
}
try {
int majority = getMajority_2(numbers);
if(!invalidArgument && majority == expected)
System.out.print("Solution2 Passed.\n");
else
System.out.print("Solution2 FAILED.\n");
}
catch(IllegalArgumentException e){
if(invalidArgument)
System.out.print("Solution2 Passed.\n");
else
System.out.print("Solution2 FAILED.\n");
}
}
private static void test1(){
int[] numbers = {1, 2, 3, 2, 2, 2, 5, 4, 2};
test("Test1", numbers, 2, false);
}
private static void test2(){
int[] numbers = {1, 2, 3, 2, 4, 2, 5, 2, 3};
test("test2", numbers, 0, true);
}
private static void test3(){
int[] numbers = {2, 2, 2, 2, 2, 1, 3, 4, 5};
test("test3", numbers, 2, false);
}
private static void test4(){
int[] numbers = {1, 3, 4, 5, 2, 2, 2, 2, 2};
test("test4", numbers, 2, false);
}
private static void test5(){
int[] numbers = {1};
test("test4", numbers, 1, false);
}
public static void main(String[] argv){
test1();
test2();
test3();
test4();
test5();
}
}