File tree 1 file changed +65
-0
lines changed
1 file changed +65
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int maxSubArray (int [] nums ) {
3
+
4
+ int n = nums .length ;
5
+ int i =0 ,sum =0 ,max =Integer .MIN_VALUE ;
6
+ int cur ,next =-1 ;
7
+
8
+ i = nextpos (nums ,0 ,n );
9
+ cur =i ;
10
+
11
+ while (i <n )
12
+ {
13
+ //System.out.println(sum+" "+max);
14
+ sum +=nums [i ];
15
+
16
+ if (sum >max )
17
+ {
18
+ max =sum ;
19
+ }
20
+
21
+ if (nums [i ]>0 && cur >=next )
22
+ {
23
+ next =i ;
24
+ }
25
+
26
+ if (sum < 0 )
27
+ {
28
+ sum =0 ;
29
+ if (cur <next )
30
+ {
31
+ cur =next ;
32
+ next =-1 ;
33
+ i =cur ;
34
+ i --;
35
+ }
36
+ }
37
+ i ++;
38
+
39
+ }
40
+
41
+ int lar = Integer .MIN_VALUE ;
42
+
43
+ for (int j =0 ;j <n ;j ++)
44
+ {
45
+ if (lar <nums [j ])
46
+ {
47
+ lar =nums [j ];
48
+ }
49
+ }
50
+
51
+ return Math .max (max ,lar );
52
+
53
+ }
54
+
55
+ public int nextpos (int arr [], int ind , int n )
56
+ {
57
+ for (int i =ind ;i <n ;i ++)
58
+ {
59
+ if (arr [i ]>0 )
60
+ return i ;
61
+ }
62
+
63
+ return n ;
64
+ }
65
+ }
You can’t perform that action at this time.
0 commit comments