-
Notifications
You must be signed in to change notification settings - Fork 154
/
MaxSliceSum_Solution_2.java
60 lines (51 loc) · 2.05 KB
/
MaxSliceSum_Solution_2.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
// This solution is a bit "ugly" (but 100%/100% correctness/performance)
package MaxSliceSum;
class Solution {
public int solution(int[] A) {
// main idea:
// use "golden slice algorithm" O(n)
// take maxEnding[i] = Math.max( 0, maxEnding[i-1] + A[i] ) <--- important~!!
// explanation :
// At the end of each slice, we decide whether its value
// is going to be carried to the next element's computation
// based on whether the value is "negative or positive". <--- "key point"
// If positive, we carry it (so it contributes to the next slice)
// Otherwise we start from "0"
// need to be careful about special cases
// special case 1: one element
if(A.length ==1)
return A[0];
// special case 2: all the elements are "negative"
// for case 2: the maximum is equal to the "single max element"
boolean negtiveCase = true;
for(int i=0; i< A.length; i++){
if(A[i] > 0)
negtiveCase = false;
}
if( negtiveCase == true){
int max = Integer.MIN_VALUE; // use "Integer.MIN_VALUE"
for(int i=0; i<A.length; i++){
if(A[i] > max)
max = A[i];
}
return max;
}
// 1) find maxEnding[]
int maxEnding[] = new int[A.length];
if(A[0] < 0) // <--- very important (be careful)
maxEnding[0] = 0;
else
maxEnding[0] = A[0];
for(int i=1; i<A.length; i++){
maxEnding[i] = Math.max( 0, maxEnding[i-1] + A[i] );
}
// 2) find max slice <--- very important (be careful)
// "not" just return maxEnding[i]; instead, we need to find the "max slice"
int maxSlice = Integer.MIN_VALUE; // <--- be careful (cannot use "0")
for(int i=0; i<A.length; i++){
if(maxEnding[i] > maxSlice)
maxSlice = maxEnding[i];
}
return maxSlice;
}
}