-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
38 lines (34 loc) · 838 Bytes
/
Copy pathmain.cpp
File metadata and controls
38 lines (34 loc) · 838 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
31
32
33
34
35
36
37
38
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int maxAscendingSum(vector<int>& nums)
{
int n = (int)nums.size();
if (n == 0)
return 0;
else if (n == 1)
return nums[0];
int sum = nums[0]; // current sum
int maximal = nums[0]; // maximal sum or maximal single element
for (int i = 1; i < n; ++i)
{
maximal = max(maximal, nums[i]);
if (nums[i] > nums[i - 1])
sum += nums[i];
else
{
maximal = max(maximal, sum);
sum = nums[i];
}
}
return max(maximal, sum);
}
};
int main()
{
vector<int> nums = {100, 10, 1};
cout << "output: " << Solution().maxAscendingSum(nums) << '\n';
return 0;
}