-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP53.cpp
45 lines (40 loc) · 817 Bytes
/
P53.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
#include<algorithm>
#include<vector>
#include<iostream>
#include<string>
#include<stack>
using namespace std;
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int sum=0;
int tmp=0;
int m=nums[0];
for (auto v:nums) {
if (tmp+v<0) {
tmp=0;
}
else {
tmp+=v;
if (tmp>sum) sum=tmp;
}
if (v>m) m=v;
}
if (m<0) sum=m;
return sum;
}
};
static const auto io_sync_off = []()
{
// turn off sync
std::ios::sync_with_stdio(false);
// untie in/out streams
std::cin.tie(nullptr);
return nullptr;
}();
int main() {
vector<int> a={-1,-1};
auto res = Solution().maxSubArray(a);
cout << res <<endl;
return 0;
}