1800. Maximum Ascending Subarray Sum - Explanation
Description
You are given an array of positive integers nums, return the maximum possible sum of an strictly increasing subarray in nums.
A subarray is defined as a contiguous sequence of numbers in an array.
Note: An array is said to be strictly increasing if each element is strictly greater than its previous one (if exists).
Example 1:
Input: nums = [10,20,30,5,10,50]
Output: 65Explanation: [5,10,50] is the ascending subarray with the maximum sum of 65.
Example 2:
Input: nums = [10,20,30,40,50]
Output: 150Explanation: [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: 33Explanation: [10,11,12] is the ascending subarray with the maximum sum of 33.
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 100
Topics
Prerequisites
Before attempting this problem, you should be comfortable with:
- Array Traversal - Iterating through arrays and comparing adjacent elements
- Subarray Concepts - Understanding contiguous sequences within an array
- Running Sum - Maintaining a cumulative sum while iterating through elements
1. Brute Force
Intuition
An ascending subarray is a contiguous sequence where each element is strictly greater than the previous. We want the maximum sum among all such subarrays.
The brute force approach considers every possible starting position. From each start, we extend the subarray as long as elements keep increasing, accumulating the sum. We track the maximum sum seen across all starting positions.
Algorithm
- Initialize
res = 0to store the maximum sum found. - For each starting index
i:- Initialize
curSum = nums[i]. - Extend from
j = i + 1whilenums[j] > nums[j-1]:- Add
nums[j]tocurSum.
- Add
- Update
res = max(res, curSum).
- Initialize
- Return
res.
Time & Space Complexity
- Time complexity:
- Space complexity:
2. Iteration
Intuition
We can solve this in a single pass. As we scan through the array, we maintain a running sum of the current ascending subarray. Whenever we encounter an element that does not continue the ascending pattern, we reset the running sum and start a new subarray from that element.
This works because ascending subarrays are naturally separated by positions where the ascending condition breaks.
Algorithm
- Initialize
res = nums[0]andcurSum = nums[0]. - For each index
ifrom1ton-1:- If
nums[i] <= nums[i-1], resetcurSum = 0. - Add
nums[i]tocurSum. - Update
res = max(res, curSum).
- If
- Return
res.
Time & Space Complexity
- Time complexity:
- Space complexity:
Common Pitfalls
Using Non-Strict Inequality
The problem requires strictly ascending elements, meaning each element must be greater than the previous one. Using >= instead of > when checking the ascending condition will incorrectly include equal consecutive elements in your subarray, leading to wrong answers.
Forgetting Single-Element Subarrays
A single element by itself forms a valid ascending subarray. If you forget to consider this case or initialize your result incorrectly, you might miss the maximum when the answer is a single large element surrounded by non-ascending neighbors.
Sign in to join the discussion