-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob_53.java
58 lines (56 loc) · 1.6 KB
/
prob_53.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
public class prob_53 {
public static void main(String args[]) {
int[] nums = {-2,1,-3,4,-1,2,1,-5,4};
System.out.println(Solution_53.maxSubArray(nums));
}
}
class Solution_53 {
public static int maxSubArray(int[] nums) {
int max;
if(nums[0]<0){
max = nums[0];
int i = 1;
for(i=1; i<nums.length; i++) {
if(nums[i]<0)
max = Math.max(nums[i], max);
else
return computeMaxSubArray(nums, i);
}
return max;
}
else
return computeMaxSubArray(nums, 0);
}
private static int computeMaxSubArray(int[] nums, int index) {
int tmp_max = 0, pos = 0, max = 0;
Boolean flag = true;
for(int i=index; i<nums.length; i++) {
if(nums[i]<0) {
if(flag) {
if(pos > tmp_max) {
tmp_max = pos;
max = Math.max(pos, max);
}
max = Math.max(max, tmp_max);
pos = 0;
flag = false;
}
tmp_max += nums[i];
}
else {
if(!flag)
flag = true;
pos+=nums[i];
tmp_max+=nums[i];
}
}
if(flag) {
if(pos > tmp_max) {
tmp_max = pos;
max = Math.max(pos, max);
}
max = Math.max(max, tmp_max);
}
return max;
}
}