-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxSubarray.java
152 lines (129 loc) · 3.16 KB
/
MaxSubarray.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
package com.gyanblog.sort;
public class MaxSubarray {
public static class MaxSubarrayData {
private int lindex;
private int rindex;
private int sum;
public MaxSubarrayData(int lindex, int rindex, int sum) {
super();
this.lindex = lindex;
this.rindex = rindex;
this.sum = sum;
}
public int getLindex() {
return lindex;
}
public void setLindex(int lindex) {
this.lindex = lindex;
}
public int getRindex() {
return rindex;
}
public void setRindex(int rindex) {
this.rindex = rindex;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("sum=").append(this.sum);
sb.append(",").append("leftIndex=").append(this.lindex);
sb.append(",").append("rightIndex=").append(this.rindex);
return sb.toString();
}
}
public static MaxSubarrayData calculate_bad(int[] a) {
int maxSum = Integer.MIN_VALUE;
int len = a.length;
MaxSubarrayData result = new MaxSubarrayData(0, 0, a[0]);
for (int i=0; i<len; i++) {
int sum = 0;
for (int j=i; j<len; j++) {
sum += a[j];
if (sum > maxSum) {
maxSum = sum;
result.setLindex(i);
result.setRindex(j);
result.setSum(maxSum);
}
}
}
return result;
}
public static MaxSubarrayData calculate_good_crossing(int[] arr, int l, int m, int r) {
MaxSubarrayData res = new MaxSubarrayData(l, r, 0);
int maxLeft = 0;
int sum = 0;
for (int i=m; i>=l; i--) {
sum += arr[i];
if (sum > maxLeft) {
maxLeft = sum;
res.setLindex(i);
}
}
int maxRight = 0;
sum = 0;
for (int i=m+1; i<=r; i++) {
sum += arr[i];
if (sum > maxRight) {
maxRight = sum;
res.setRindex(i);
}
}
res.setSum(maxLeft + maxRight);
return res;
}
public static MaxSubarrayData calculate_good(int[] arr, int l, int r) {
if (l == r) {
return new MaxSubarrayData(l, r, arr[l]);
}
else {
int m = (l+r)/2;
MaxSubarrayData left = calculate_good(arr, l, m);
MaxSubarrayData right = calculate_good(arr, m+1, r);
MaxSubarrayData crossing = calculate_good_crossing(arr, l, m, r);
if (left.getSum() > right.getSum() && left.getSum() > crossing.getSum()) {
return left;
}
else if (right.getSum() > left.getSum() && right.getSum() > crossing.getSum()) {
return right;
}
else {
return crossing;
}
}
}
public static MaxSubarrayData calculate_best(int[] arr) {
MaxSubarrayData res = new MaxSubarrayData(0, 0, arr[0]);
int maxSum = 0;
int sum = 0;
int len = arr.length;
for (int i=0; i<len; i++) {
sum += arr[i];
if (sum > maxSum) {
maxSum = sum;
res.setRindex(i);
res.setSum(maxSum);
}
if (sum < 0) {
sum = 0;
res.lindex = i+1;
}
}
return res;
}
public static void main(String[] args) {
int[] arr = {-2, -3, 4, -1, -2, 1, 5, -3};
MaxSubarrayData res = calculate_bad(arr);
System.out.println(res);
MaxSubarrayData res_best = calculate_best(arr);
System.out.println(res_best);
MaxSubarrayData res_good = calculate_good(arr, 0, arr.length-1);
System.out.println(res_good);
}
}