-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path53.最大子数组和.cpp
84 lines (83 loc) · 1.64 KB
/
53.最大子数组和.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* @lc app=leetcode.cn id=53 lang=cpp
*
* [53] 最大子数组和
*
* https://leetcode.cn/problems/maximum-subarray/description/
*
* algorithms
* Medium (54.93%)
* Likes: 6415
* Dislikes: 0
* Total Accepted: 1.5M
* Total Submissions: 2.8M
* Testcase Example: '[-2,1,-3,4,-1,2,1,-5,4]'
*
* 给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
*
* 子数组 是数组中的一个连续部分。
*
*
*
* 示例 1:
*
*
* 输入:nums = [-2,1,-3,4,-1,2,1,-5,4]
* 输出:6
* 解释:连续子数组 [4,-1,2,1] 的和最大,为 6 。
*
*
* 示例 2:
*
*
* 输入:nums = [1]
* 输出:1
*
*
* 示例 3:
*
*
* 输入:nums = [5,4,-1,7,8]
* 输出:23
*
*
*
*
* 提示:
*
*
* 1 <= nums.length <= 10^5
* -10^4 <= nums[i] <= 10^4
*
*
*
*
* 进阶:如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的 分治法 求解。
*
*/
#include <bits/stdc++.h>
using namespace std;
// @lc code=start
class Solution {
public:
int maxSubArray(vector<int> &nums) {
vector<int> dp(nums.size(), -1);
dp[0] = nums[0];
int ans = dp[0];
for (int i = 1; i < nums.size(); i++) {
if (dp[i - 1] + nums[i] > nums[i]) {
dp[i] = dp[i - 1] + nums[i];
} else {
dp[i] = nums[i];
}
ans = max(ans, dp[i]);
}
return ans;
}
};
// @lc code=end
int main() {
vector<int> input = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
cout << Solution().maxSubArray(input) << endl;
return 0;
}