LeetCode刷题笔记第1800题:最大升序子数组和
题目:
想法:
遍历数组的同时记录当前最大升序子数组和,最终返回最大升序子数组和
class Solution:def maxAscendingSum(self, nums: List[int]) -> int:result = 0i = 0n = len(nums)while i < n:s = nums[i]i += 1while i < n and nums[i] > nums[i - 1]:s += nums[i]i += 1result = max(result, s)return result
因为要遍历整个数组,时间复杂度为O(n)
因为要存储当前最大升序子数组和,空间复杂度为O(1)