-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl53_maximum_subarray.cpp
More file actions
56 lines (47 loc) · 1.22 KB
/
l53_maximum_subarray.cpp
File metadata and controls
56 lines (47 loc) · 1.22 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
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
class L53MaximumSubArray {
public:
static int max_sub_array(vector<int>& nums) {
int biggest = nums[0];
if (nums.size() < 2)
return biggest;
int left = 0;
int temp = nums[0];
int prev = nums[0];
for (int right = 1; right < nums.size(); ++right) {
temp += nums[right];
if (temp > biggest) {
biggest = temp;
prev = temp;
}
else if (temp < prev)
{
left = right;
prev = nums[left];
temp = nums[left];
}
else
{
prev = temp;
}
}
return biggest;
}
int max_sub_array_solution(vector<int>& nums) {
int n = size(nums), ans = INT_MIN;
for(int i = 0; i < n; i++)
for(int j = i, curSum = 0; j < n ; j++)
curSum += nums[j],
ans = max(ans, curSum);
return ans;
}
};
int main() {
vector<int> n{-2,1,-3,4,-1,2,1,-5,4};
int out = L53MaximumSubArray::max_sub_array(n);
cout << out << endl;
return 0;
}