Solution
- Time Complexity: O(n)
- Space Complexity: O(1)
func maxAscendingSum(nums []int) int {
sum := nums[0]
maxSum := nums[0]
for i := 1; i < len(nums); i++ {
if nums[i] > nums[i-1] {
sum += nums[i]
} else {
sum = nums[i]
}
if sum > maxSum {
maxSum = sum
}
}
return maxSum
}Explanation
The problem requires finding the maximum sum of a strictly ascending subarray within the given array. A subarray is a contiguous part of the array. An ascending subarray means that each element is strictly greater than the one before it.
The solution involves iterating over the array and keeping a running sum of the current ascending subarray. When a number is encountered that is not greater than its previous number, the current subarray ends, and we start a new subarray from that number. During each iteration, we update the maximum sum if the current subarray's sum is greater than the previously recorded maximum.
Approach
- Step 1: Initialize two variables:
sumto keep the running total of the current ascending subarray, andmaxSumto record the maximum sum found so far. Both are initialized to the first element of the array. - Step 2: Loop through the array starting from the second element.
- Step 3: For each element, check if it is strictly greater than the previous element.
- If yes, add the current element to
sumbecause the subarray is still ascending. - If no, reset
sumto the current element, as this marks the beginning of a new potential ascending subarray.
- If yes, add the current element to
- Step 4: Update
maxSumwith the maximum ofmaxSumandsumat every iteration. - Step 5: After completing the iteration through the array, return
maxSumas the result.
Example Walkthrough
Example 1:
Input: [10, 20, 30, 5, 10]
-
Initialization:
sum = 10,maxSum = 10 -
Iteration:
- For
i = 1(20):20 > 10, so updatesum = 10 + 20 = 30- Update
maxSum = max(10, 30) = 30
- For
i = 2(30):30 > 20, so updatesum = 30 + 30 = 60- Update
maxSum = max(30, 60) = 60
- For
i = 3(5):5 <= 30, so resetsum = 5maxSumremains60
- For
i = 4(10):10 > 5, so updatesum = 5 + 10 = 15maxSumremains60
- For
-
Result:
The maximum ascending subarray sum is60.
Example 2:
Input: [10, 20, 30, 40, 50]
-
Initialization:
sum = 10,maxSum = 10 -
Iteration:
- For each subsequent element, the condition holds as each number is greater than the previous one:
sumbecomes10 + 20 = 30, then30 + 30 = 60, then60 + 40 = 100, and finally100 + 50 = 150.maxSumis updated in each iteration to finally be150.
- For each subsequent element, the condition holds as each number is greater than the previous one:
-
Result:
The maximum ascending subarray sum is150.
Complexity Analysis
-
Time Complexity:
The algorithm processes each element in the array exactly once in a single loop, resulting in a time complexity of O(n). -
Space Complexity:
The solution uses only a constant amount of extra space (for the variablessumandmaxSum), resulting in a space complexity of O(1).

