-
Notifications
You must be signed in to change notification settings - Fork 113
/
ArrayMaxset.java
67 lines (61 loc) · 1.79 KB
/
ArrayMaxset.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
import java.util.ArrayList;
public class ArrayMaxset{
public static ArrayList<Integer> maxset(ArrayList<Integer> A) {
ArrayList<Integer> B =new ArrayList<Integer>();
int max = 0;
int total = 0;
for(int i = 0; i < A.size(); i++){
total += A.get(i);
if(total > max) {
max = total;
B.add(A.get(i));
}
}
return B;
}
public static ArrayList<Integer> maxsetSolution(ArrayList<Integer> A) {
ArrayList<Integer> B =new ArrayList<Integer>();
int maxSum = A.get(0);
int currSum = A.get(0);
B.add(A.get(0));
for(int i = 1; i<A.size(); i++){
if(A.get(i) >= 0){
currSum = currSum + A.get(i);
if(maxSum <= currSum){
maxSum = currSum;
B.add(A.get(i));
}
}
else{
currSum = 0;
//B.clear();
}
}
return B;
}
public static int maxSubArray(ArrayList<Integer> A) {
int maxi = A.get(0);
int i;
int cur = A.get(0);
for(i = 1; i < A.size(); i++){
cur = Math.max(A.get(i), cur+A.get(i));
maxi = Math.max(maxi, cur);
}
return maxi;
}
public static void main(String[] args){
ArrayList<Integer> A =new ArrayList<Integer>();
A.add(1);
A.add(2);
A.add(3);
A.add(-7);
A.add(2);
A.add(3);
// System.out.println();
// System.out.println(maxSubArray(A));
ArrayList<Integer> B = maxsetSolution(A);
for (int i = 0; i < B.size(); i++) {
System.out.print(B.get(i) + " ");
}
}
}