-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumsubarraysum.cpp
More file actions
55 lines (47 loc) · 1.36 KB
/
Maximumsubarraysum.cpp
File metadata and controls
55 lines (47 loc) · 1.36 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
#include<iostream>
#include <string>
#include<vector>
using namespace std;
int maxSequence(const vector<int>& arr) {
int max_so_far = 0;
int current_max = 0;
for (int i = 0; i < arr.size(); ++i) {
current_max = current_max + arr[i];
if (current_max < 0) {
current_max = 0;
}
if (current_max > max_so_far) {
max_so_far = current_max;
}
}
return max_so_far;
}
int main()
{
cout << maxSequence({ -2, 1, -3, 4, -1, 2, 1, -5, 4 });
return 0;
}
/*Description:
The maximum sum subarray problem consists in finding the maximum
sum of a contiguous subsequence in an array or list of integers:
maxSequence({-2, 1, -3, 4, -1, 2, 1, -5, 4});
//should be 6: {4, -1, 2, 1}
Easy case is when the list is made up of only positive numbers and the maximum sum is the sum of the whole array.
If the list is made up of only negative numbers, return 0 instead.
Empty list is considered to have zero greatest sum.
Note that the empty list or array is also a valid sublist/subarray.
Algorithms
Lists
Dynamic Programming
Fundamentals
Performance*/
// /> フ
// | n n 彡
// /`ミ_xノ
// / |
// / ヽ ノ
// │ | | |
// / ̄| | | |
// | ( ̄ヽ__ヽ_)__)
// \二つ
// ITS CAT FOR YOU