-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path53.maximum-subarray.cpp
60 lines (51 loc) · 958 Bytes
/
53.maximum-subarray.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
/*
* @lc app=leetcode.cn id=53 lang=cpp
* @lcpr version=30109
*
* [53] 最大子数组和
*/
// @lcpr-template-start
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
// @lcpr-template-end
// @lc code=start
class Solution {
public:
int maxSubArray(vector<int>& nums)
{
int ans=nums[0];
int trav=nums[0];
for(int i=1;i<nums.size();i++)
{
trav=max(trav+nums[i],nums[i]);
ans=max(ans,trav);
}
return ans;
}
};
// @lc code=end
/*
// @lcpr case=start
// [-2,1,-3,4,-1,2,1,-5,4]\n
// @lcpr case=end
// @lcpr case=start
// [1]\n
// @lcpr case=end
// @lcpr case=start
// [5,4,-1,7,8]\n
// @lcpr case=end
*/