-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_1186_maximumSum.cc
65 lines (59 loc) · 1.34 KB
/
Problem_1186_maximumSum.cc
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
#include <algorithm>
#include <iostream>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
int maximumSum(vector<int> &arr)
{
int n = arr.size();
// dp[i][j]的含义为以 arr[i] 结尾(删除前),删除 j 次的非空子数组的最大和
vector<vector<int>> dp(n, vector<int>(2));
dp[0][0] = arr[0];
dp[0][1] = 0;
int ans = arr[0];
for (int i = 1; i < n; i++)
{
dp[i][0] = std::max(dp[i - 1][0], 0) + arr[i];
dp[i][1] = std::max(dp[i - 1][1] + arr[i], dp[i - 1][0]);
ans = std::max(ans, std::max(dp[i][0], dp[i][1]));
}
return ans;
}
// 空间优化
int dp(vector<int> &arr)
{
int n = arr.size();
int dp0 = arr[0];
int dp1 = 0;
int ans = arr[0];
for (int i = 1; i < n; i++)
{
dp1 = std::max(dp1 + arr[i], dp0);
dp0 = std::max(dp0, 0) + arr[i];
ans = std::max(ans, std::max(dp0, dp1));
}
return ans;
}
};
void test()
{
Solution s;
vector<int> n1 = {1, -2, 0, 3};
vector<int> n2 = {1, -2, -2, 3};
vector<int> n3 = {-1, -1, -1, -1};
EXPECT_EQ_INT(4, s.maximumSum(n1));
EXPECT_EQ_INT(3, s.maximumSum(n2));
EXPECT_EQ_INT(-1, s.maximumSum(n3));
EXPECT_EQ_INT(4, s.dp(n1));
EXPECT_EQ_INT(3, s.dp(n2));
EXPECT_EQ_INT(-1, s.dp(n3));
EXPECT_SUMMARY;
}
int main()
{
test();
return 0;
}