LeetCode Problem Workspace
Maximum Ascending Subarray Sum
Solve Maximum Ascending Subarray Sum by scanning once, extending rising runs, and resetting the running sum at each drop.
1
Topics
7
Code langs
3
Related
Practice Focus
Easy · Array-driven solution strategy
Answer-first summary
Solve Maximum Ascending Subarray Sum by scanning once, extending rising runs, and resetting the running sum at each drop.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Array-driven solution strategy
Maximum Ascending Subarray Sum is an easy one-pass array problem: keep a running sum while the sequence stays strictly increasing, and reset when it breaks. The key detail is contiguity, so you are not building a subsequence or trying every combination. For nums = [10,20,30,5,10,50], the best rising run is [5,10,50], which gives 65.
Problem Statement
You are given an array of positive integers and need the largest sum among all contiguous subarrays whose values increase strictly from left to right. Because the subarray must stay contiguous, once the current value is not greater than the previous one, the current ascending run ends and a new run must begin at that position.
For example, in nums = [10,20,30,5,10,50], the run [10,20,30] sums to 60, but the later run [5,10,50] sums to 65, so the answer is 65. The array length is between 1 and 100, each value is between 1 and 100, and examples like a fully increasing array or a drop in the middle make it clear that the decision point is exactly where the ascending streak breaks.
Examples
Example 1
Input: nums = [10,20,30,5,10,50]
Output: 65
[5,10,50] is the ascending subarray with the maximum sum of 65.
Example 2
Input: nums = [10,20,30,40,50]
Output: 150
[10,20,30,40,50] is the ascending subarray with the maximum sum of 150.
Example 3
Input: nums = [12,17,15,13,10,11,12]
Output: 33
[10,11,12] is the ascending subarray with the maximum sum of 33.
Constraints
- 1 <= nums.length <= 100
- 1 <= nums[i] <= 100
Solution Approach
Track one ascending run at a time
Walk from left to right with two values: the sum of the current strictly increasing contiguous run and the best sum seen so far. Start both with nums[0], because even a single element is a valid ascending subarray.
Extend on a strict increase, reset on a break
At index i, compare nums[i] with nums[i-1]. If nums[i] > nums[i-1], extend the current run by adding nums[i]. Otherwise, the ascending condition fails, so reset the current run sum to nums[i] and treat this element as the start of a new candidate subarray.
Why brute force is unnecessary here
It is fast enough to check all possible subarrays under these small constraints, but this problem has a cleaner array-driven solution because every valid ascending subarray belongs to a maximal rising run. Once a run breaks, no earlier prefix can be salvaged across that break, so a single pass captures the best answer in O(n) time and O(1) space.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | O(n) |
| Space | O(1) |
The scan touches each element once, so the time complexity is O(n). It only stores the current ascending-run sum and the maximum sum found so far, so the space complexity is O(1).
What Interviewers Usually Probe
- The interviewer emphasizes contiguous subarray, which points to tracking runs instead of searching arbitrary subsequences.
- The constraint that values are positive makes the running-sum reset logic especially straightforward when an ascending streak breaks.
- A hint like "fast enough to check all possible subarrays" often tests whether you can notice the simpler one-pass property of maximal increasing runs.
Common Pitfalls or Variants
Common pitfalls
- Using >= instead of >, which incorrectly merges equal adjacent values into an ascending run.
- Thinking this is about the longest or maximum-sum increasing subsequence, even though the subarray must remain contiguous.
- Forgetting to update the best sum after extending a run, which can miss answers in a fully increasing array such as [10,20,30,40,50].
Follow-up variants
- Return the actual ascending subarray with the maximum sum instead of only the sum, which requires tracking the run start and best segment bounds.
- Change the rule to non-decreasing subarray sum, where the comparison becomes >= and equal neighbors no longer break the run.
- Ask for the maximum product of a strictly increasing contiguous subarray, which changes the state update even though the run-breaking logic stays similar.
FAQ
What is the main pattern behind Maximum Ascending Subarray Sum?
The pattern is a one-pass array scan over contiguous increasing runs. You keep adding while nums[i] > nums[i-1], and when that condition fails, you reset the running sum to start a new run.
Why is the answer 65 for nums = [10,20,30,5,10,50]?
The ascending runs are [10,20,30] with sum 60 and [5,10,50] with sum 65. Since the second strictly increasing contiguous run has the larger sum, the result is 65.
Why is this not an increasing subsequence problem?
Because the problem asks for a subarray, which must be contiguous. You cannot skip elements, so once the ascending order breaks between adjacent positions, that run is over.
Can I solve this by checking all subarrays?
Yes, the constraints are small enough that brute force can pass. But the intended trade-off is recognizing that only contiguous rising runs matter, which gives a simpler O(n) solution with constant extra space.
What breaks the current run in this array pattern?
Any position where nums[i] <= nums[i-1] ends the current strictly increasing subarray. At that point, the new current sum must restart from nums[i] rather than continue across the break.
Solution
Solution 1: Direct Simulation
We use a variable $t$ to record the current sum of the ascending subarray, and a variable $ans$ to record the maximum sum of the ascending subarray.
class Solution:
def maxAscendingSum(self, nums: List[int]) -> int:
ans = t = 0
for i, v in enumerate(nums):
if i == 0 or v > nums[i - 1]:
t += v
ans = max(ans, t)
else:
t = v
return ansContinue Topic
array
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Array-driven solution strategy
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward