题目描述
blablabla
样例
blablabla
写法1
/*
时间复杂度:O(n^2)
*/
class Solution {
public:
int maxAscendingSum(vector<int>& nums) {
int n = nums.size();
int res = 0, sum = 0;
for (int i = 0; i < n; i ++) {
sum = nums[i];
res = max(res, sum);
for (int j = i + 1; j < n; j ++) {
if (nums[j] > nums[i]) {
sum += nums[j];
res = max(res, sum);
i = j;
} else {
sum = 0;
break;
}
}
}
return res;
}
};
写法2
/*
时间复杂度:O(n)
*/
class Solution {
public:
int maxAscendingSum(vector<int>& nums) {
int n = nums.size();
int s = nums[0], res = nums[0];
for (int i = 1; i < n; i ++) {
if (nums[i] > nums[i - 1]) s += nums[i];
else s = nums[i];
if (res < s) res = s;
}
return res;
}
};