forked from LuYanFCP/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path53.cpp
More file actions
41 lines (38 loc) · 1.04 KB
/
Copy path53.cpp
File metadata and controls
41 lines (38 loc) · 1.04 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
#include <vector>
#include <cmath>
#include <cstdio>
using std::vector;
using std::printf;
#define MAX(a, b) (a) > (b) ? (a) : (b)
class Solution {
public:
int maxSubArray(vector<int>& nums) {
/*
* 4 ms
* 9.4 MB
*/
int n = nums.size();
if (n == 0) {
return 0;
}
/*
* dp数组的含义:到i为结尾最大子序列和
* 转移方程: dp[i] = MAX(nums[i], nums[i] + dp[i - 1]);
* 解释,以i为结尾有两种情况 [nums[i]], [...nums[i-1], nums[i]]
*/
vector<int> dp(n); // 这个其实可以直接使用nums代替,这里使用dp是为了更好理解
dp[0] = nums[0];
int max_sum = nums[0];
for (int i = 1; i < n; i++) {
dp[i] = MAX(nums[i], nums[i] + dp[i - 1]);
max_sum = MAX(dp[i], max_sum);
}
return max_sum;
}
};
int main()
{
Solution s;
vector<int> vec = {-2, -1};
printf("%d", s.maxSubArray(vec));
}