-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxValue.java
50 lines (41 loc) · 1.31 KB
/
MaxValue.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
package array.recursive;
import utils.constants.Test;
import java.util.ArrayList;
import java.util.Arrays;
public class MaxValue {
/*
method: divide and conquer
*/
public static <T extends Comparable<T>> T getMaxValueDC(ArrayList<T> v) {
return getMaxValueDC(v, 0, v.size() - 1);
}
private static <T extends Comparable<T>> T getMaxValueDC(ArrayList<T> v, int start, int end) {
if (start == end) {
return v.get(start);
}
int m = (start + end) / 2;
T left = getMaxValueDC(v, start, m);
T right = getMaxValueDC(v, m + 1, end);
if (left.compareTo(right) > 0) {
return left;
} else {
return right;
}
}
public static <T extends Comparable<T>> T getMaxValue(ArrayList<T> v) {
if (v.size() == 1) {
return v.get(0);
}
T t = v.remove(v.size() - 1); // (right ?)
T max = getMaxValue(v);
if (max.compareTo(t) > 0) {
return max;
} else {
return t;
}
}
public static void main(String[] args) {
System.out.println(MaxValue.getMaxValue(new ArrayList<Integer>(Arrays.asList(Test.V))));
System.out.println(MaxValue.getMaxValueDC(new ArrayList<Integer>(Arrays.asList(Test.V))));
}
}