[LeetCode] Maximum Ascending Subarray Sum

1800. Maximum Ascending Subarray Sum

Given an array of positive integers nums, return the maximum possible sum of an ascending subarray in nums.

A subarray is defined as a contiguous sequence of numbers in an array.

A subarray [numsl, numsl+1, ..., numsr-1, numsr] is ascending if for all i where l <= i < r, numsi < numsi+1. Note that a subarray of size 1 is ascending.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int maxAscendingSum(vector<int>& nums) {
int res = nums[0], now = nums[0];
for(int i = 1; i < nums.size(); i++) {
if(nums[i] > nums[i-1]) now += nums[i];
else now = nums[i];
res = max(res, now);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/02/04/PS/LeetCode/maximum-ascending-subarray-sum/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.