-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmax_subarray_sum.cpp
63 lines (51 loc) · 1.54 KB
/
max_subarray_sum.cpp
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
#include <vector>
#include <climits>
#include <iostream>
using namespace std;
//O(n) Kadane's Algorithm (positive and negative values)
int max_subarray_sum(const vector<int> & vec, const int N)
{
int max_so_far = INT_MIN;
int max_ending_here = 0;
int idx_start = 0, idx_end = 0, idx_temp = 0;
for (int i=0; i < N; ++i)
{
max_ending_here = max_ending_here + vec[i];
if (max_ending_here > max_so_far)
{
max_so_far = max_ending_here;
idx_start = idx_temp;
idx_end = i;
}
if (max_ending_here < 0) {max_ending_here = 0; idx_temp = i + 1;}
}
cout << "idx start: " << idx_start << ", " << "idx end: " << idx_end << endl;
return max_so_far;
}
//O(n) dynamic programming (handles the case of all negative values)
int max_subarray_sum_dp(const vector<int> & vec, const int N)
{
int max_so_far = vec[0];
int curr_max = vec[0];
for (int i=0; i < N; ++i)
{
curr_max = max(vec[i], curr_max + vec[i]);
max_so_far = max(max_so_far, curr_max);
}
return max_so_far;
}
int main()
{
int arr[] = {-2, -3, 4, -1, -2, 1, 5, -3};
int N = sizeof(arr)/sizeof(arr[0]);
vector<int> vec (arr, arr+N);
for (auto item : vec){cout << item << " ";}
cout << endl;
int sum_kadane = 0;
sum_kadane = max_subarray_sum(vec, N);
int sum_dp = 0;
sum_dp = max_subarray_sum_dp(vec, N);
cout << "max subarray sum (Kadane): " << sum_kadane << endl;
cout << "max subarray sum (DP): " << sum_dp << endl;
return 0;
}