-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
30 lines (29 loc) · 764 Bytes
/
Copy pathsolution.cpp
File metadata and controls
30 lines (29 loc) · 764 Bytes
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
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int maxSumIS(vector<int> &arr)
{
int n = arr.size();
if (n == 0)
return 0;
// dp[i] = max sum of increasing subsequence ending at i
vector<int> dp(n);
int ans = 0;
for (int i = 0; i < n; ++i)
{
dp[i] = arr[i]; // at least the element itself
// try to extend subsequence that ends at j (< i)
for (int j = 0; j < i; ++j)
{
if (arr[j] < arr[i])
{
dp[i] = max(dp[i], dp[j] + arr[i]);
}
}
ans = max(ans, dp[i]);
}
return ans;
}
};