Maximum Ascending Subarray Sum — LeetCode 1800 Python Solution

EasyArray
Problem
#1800
Reading time
2 min

The problem

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.

Example

Input
nums = [10,20,30,5,10,50]
Output
65
Explanation
[5,10,50] is the ascending subarray with the maximum sum of 65.

Python solution

Python
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 ans

Complexity

MeasureComplexity
TimeO(n), where n is the length of the array nums
SpaceO(1) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 1800. Maximum Ascending Subarray Sum?
LeetCode 1800. Maximum Ascending Subarray Sum is rated Easy on LeetCode.
What is the time complexity of LeetCode 1800. Maximum Ascending Subarray Sum?
The Python solution on this page runs in O(n), where n is the length of the array nums.
What is the space complexity of LeetCode 1800. Maximum Ascending Subarray Sum?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 1800. Maximum Ascending Subarray Sum cover?
LeetCode 1800. Maximum Ascending Subarray Sum is tagged Array on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview