-
Notifications
You must be signed in to change notification settings - Fork 78
/
MaxDoubleSliceSum.cs
69 lines (65 loc) · 1.35 KB
/
MaxDoubleSliceSum.cs
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
using System;
class Solution {
public int solution(int[] A) {
int
sum = A[0] + A[1] + A[2], bestSum = sum,
start = 0, bestStart = start,
end = 2, bestEnd = end;
for (var i = end; ++i < A.Length;) {
if (A[start] < 0 && A[i] > A[start]) {
sum += A[i] - A[start];
++start;
++end;
if (sum > bestSum) {
bestSum = sum;
bestStart = start;
bestEnd = end;
}
}
else if (A[i] >= 0) {
++end;
if ((sum += A[i]) > bestSum) {
bestSum = sum;
bestStart = start;
bestEnd = end;
}
}
else {
sum += A[i];
++end;
}
}
if (A[bestStart] > 0) {
if (bestStart > 0) {
bestSum += A[--bestStart];
}
else
while (A[bestStart + 1] < 0 && bestEnd - bestStart > 2)
bestSum -= A[bestStart++];
}
if (A[bestEnd] > 0) {
if (bestEnd < A.Length - 1) {
bestSum += A[++bestEnd];
}
else {
while (A[bestEnd - 1] < 0 && bestEnd - bestStart > 2)
bestSum -= A[bestEnd--];
}
}
int smallest = bestStart + 1;
for (var i = smallest; ++i < bestEnd;)
if (A[smallest] > A[i])
smallest = i;
if (A[smallest] > 0) {
if (bestStart > 0) {
smallest = bestStart;
bestSum += A[--bestStart];
}
else if (bestEnd < A.Length - 1) {
smallest = bestEnd;
bestSum += A[++bestEnd];
}
}
return bestSum - A[bestStart] - A[bestEnd] - A[smallest];
}
}