forked from kedebug/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumSubarray.cpp
More file actions
52 lines (45 loc) · 1.3 KB
/
MaximumSubarray.cpp
File metadata and controls
52 lines (45 loc) · 1.3 KB
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
class Solution {
public:
int maxSubArray(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int maxsum = A[0];
int tempsum = A[0];
for (int i = 1; i < n; i++) {
if (tempsum < 0) tempsum = 0;
tempsum += A[i];
maxsum = max(maxsum, tempsum);
}
return maxsum;
}
};
//
// solution 2 : divide and conquer
//
class Solution {
public:
int maxSubArray(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
return maxSubArray(A, 0, n - 1);
}
int maxSubArray(int A[], int left, int right) {
if (left > right)
return INT_MIN;
int mid = (left + right) / 2;
int lmax = maxSubArray(A, left, mid - 1);
int rmax = maxSubArray(A, mid + 1, right);
int sum = 0, mlmax = 0;
for (int i = mid - 1; i >= left; i--) {
sum += A[i];
mlmax = max(mlmax, sum);
}
sum = 0;
int mrmax = 0;
for (int i = mid + 1; i <= right; i++) {
sum += A[i];
mrmax = max(mrmax, sum);
}
return max(mlmax + A[mid] + mrmax, max(lmax, rmax));
}
};