forked from livingstream/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumSubarray.cpp
More file actions
65 lines (55 loc) · 1.73 KB
/
MaximumSubarray.cpp
File metadata and controls
65 lines (55 loc) · 1.73 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
53
54
55
56
57
58
59
60
61
62
63
64
65
//============================================================================
// Find the contiguous subarray within an array (containing at least one
// number) which has the largest sum.
//
// For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
// the contiguous subarray [4,1,2,1] has the largest sum = 6.
//
// Complexity:
// Greedy O(n) time
// Divide and Conquer O(nlog(n)) time
//============================================================================
#include <iostream>
#include <climits>
using namespace std;
class Solution {
public:
int maxSubArray(int A[], int n) {
return maxSubArray2(A, n);
}
int maxSubArray1(int A[], int n) {
int sum = 0, res = INT_MIN;
for (int i = 0; i < n; i++) {
sum += A[i];
if (sum > res) res = sum;
if (sum < 0) sum = 0;
}
return res;
}
int maxSubArray2(int A[], int n) {
return maxSubArrayHelper1(A, 0, n - 1);
}
int maxSubArrayHelper1(int A[], int l, int u) {
if (l > u) return INT_MIN;
int m = l + (u - l) / 2;
int sum = 0, lmax = 0, rmax = 0;
for (int i = m - 1; i >= l; i--) sum += A[i], lmax = max(lmax, sum);
sum = 0;
for (int i = m + 1; i <= u; i++) sum += A[i], rmax = max(rmax, sum);
return max(A[m] + lmax + rmax, max(maxSubArrayHelper1(A, l, m - 1), maxSubArrayHelper1(A, m + 1, u)));
}
};
int main() {
Solution sol;
{
int p0[] = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
int p1 = sol.maxSubArray(p0, sizeof(p0) / sizeof(int));
cout << p1 << endl;
}
{
int p0[] = { -2, -1 };
int p1 = sol.maxSubArray(p0, sizeof(p0) / sizeof(int));
cout << p1 << endl;
}
return 0;
}