Maximum Ascending Subarray Sum (LeetCode 1800): Solution and Explanation

Easy
Array

Find the largest sum of a strictly increasing contiguous run. Walk the array once, adding to the current run while it climbs and restarting when it does not, and keep the largest run sum.

Intuition

Draw the array as bars. An ascending subarray is a staircase that only goes up. The array splits into maximal staircases, cut wherever a bar is not taller than the one before it.

Every ascending subarray sits inside one of those staircases. Because all values are positive, taking more of a staircase never lowers the sum, so the best choice inside a staircase is the whole staircase.

So the answer is the largest staircase sum, and one running total that resets at each cut finds it.

Approach: Running sum of climbing runs

  1. Set cur = best = nums[0].
  2. For i from 1 to n - 1: if nums[i] > nums[i - 1], add nums[i] to cur; otherwise restart with cur = nums[i].
  3. After each step, set best = max(best, cur).
  4. Return best.

Step-by-step example

nums = [10,20,30,5,10,50]
  1. Start: cur = best = 10.
  2. 20 > 10, cur = 30, best = 30. 30 > 20, cur = 60, best = 60.
  3. 5 <= 30, the climb breaks: cur = 5. best stays 60.
  4. 10 > 5, cur = 15. 50 > 10, cur = 65, best = 65.
  5. Return 65, the sum of [5,10,50].

Code: Python, Java, C++

1class Solution:
2    def maxAscendingSum(self, nums: List[int]) -> int:
3        best = cur = nums[0]
4        for i in range(1, len(nums)):
5            if nums[i] > nums[i - 1]:
6                cur += nums[i]
7            else:
8                cur = nums[i]
9            best = max(best, cur)
10        return best
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each element is looked at once.

Space

O(1)

Only cur and best are kept.

Other approaches

Try every start (time O(n^2), space O(1))

From each index, extend to the right while the values climb and sum as you go. It is correct and fine for 100 elements, but it repeats work inside each run.

Split into runs first (time O(n), space O(n))

Collect the run boundaries into a list, then sum each run and take the maximum. Same complexity with extra memory and code.

Common mistakes

  • Using >= instead of >. Equal neighbours end a run, because ascending means strictly increasing.
  • Resetting cur to 0 instead of nums[i] on a break. The breaking element starts the next run.
  • Updating best only when a run ends, which misses a best run that reaches the last element unless you check once more after the loop.

Frequently asked questions

Why is a whole run always at least as good as part of it?

All values are at least 1, so adding more elements of the same run can only raise the sum.

Does a single element count?

Yes. A subarray of length 1 is ascending, which is why best starts at nums[0] and an all-decreasing array returns its largest element.

How does this compare to Maximum Subarray?

Kadane's algorithm restarts when the running sum turns negative. Here values are positive, so the restart rule is about order instead: restart when the climb breaks.