EasyNo. 1800
Maximum Ascending Subarray Sum — LeetCode 1800 Python Solution
- Topics
- Array
- Reading time
- 2 min
- Source
- leetcode.com
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
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 ansComplexity
- Time
- O(n)where n is the length of the array nums
- Space · auxiliary
- O(1)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 0057 | Insert Interval | Medium |
| 0228 | Summary Ranges | Easy |
| 0485 | Max Consecutive Ones | Easy |
| 0665 | Non-decreasing Array | Medium |
| 0674 | Longest Continuous Increasing Subsequence | Easy |
| 0699 | Falling Squares | Hard |
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.