-
Notifications
You must be signed in to change notification settings - Fork 1
/
MaximumSubarray.h
76 lines (66 loc) · 1.98 KB
/
MaximumSubarray.h
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
70
71
72
73
74
75
76
/*
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.
click to show more practice.
More practice:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach,
which is more subtle.
Written by Xi Wang
11/17/2013
*/
class Solution {
public:
// SOUTION 1: Divide and COnquer O(NLogN)
int maxCrossArray(int A[],int l,int mid,int r)
{
if(l>r)
return 0;
int left_sum = INT_MIN, sum=0;
for(int i=mid;i>=l;i--)
{
sum +=A[i];
if(sum > left_sum)
left_sum = sum;
}
int right_sum = INT_MIN;
sum=0;
for(int i=mid+1;i<=r;i++)
{
sum +=A[i];
if(sum > right_sum)
right_sum = sum;
}
return left_sum+right_sum;
}
int maxSubArray(int A[],int l, int r)
{
if(l==r)
return A[l];
int mid = (l+r)/2;
int leftmax = maxSubArray(A,l,mid);
int rightmax = maxSubArray(A,mid+1,r);
int crossmax = maxCrossArray(A,l,mid,r);
return max(max(leftmax,rightmax),crossmax);
}
int maxSubArray(int A[], int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
return maxSubArray(A,0,n-1);
}
// solution 2: linear approach O(n)
int maxSubArray(int A[], int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int sum = 0, max = INT_MIN;
for(int i=0;i<n;i++)
{
sum +=A[i];
if(sum>max)
max = sum;
if(sum<=0)
sum=0;
}
return max;
}
};