EasyNo. 1800

Maximum Ascending Subarray Sum — LeetCode 1800 Python Solution

Topics
Array
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

PythonSolution
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

Time
O(n)where n is the length of the array nums
Space · auxiliary
O(1)

Related problems

Related problems
No.ProblemDifficulty
0057Insert IntervalMedium
0228Summary RangesEasy
0485Max Consecutive OnesEasy
0665Non-decreasing ArrayMedium
0674Longest Continuous Increasing SubsequenceEasy
0699Falling SquaresHard

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.

Free to download. Check it runs on your machine, then pick a plan.