diff options
author | Jack Sangdahl <[email protected]> | 2025-02-03 17:14:08 -0700 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-02-03 17:14:08 -0700 |
commit | c6b7c4bdc62e1b77f2f4ae405fdeb8f9fbdfa91d (patch) | |
tree | 33d2b3876af02404c91f00e343563b336bb1cea7 /max_ascending_subarr_sum.py | |
parent | 55a67f776dd9acd258cae16ffb636751666b0f40 (diff) | |
download | leetcode-c6b7c4bdc62e1b77f2f4ae405fdeb8f9fbdfa91d.tar.gz leetcode-c6b7c4bdc62e1b77f2f4ae405fdeb8f9fbdfa91d.zip |
1800. maximum ascending subarray sum
Diffstat (limited to 'max_ascending_subarr_sum.py')
-rw-r--r-- | max_ascending_subarr_sum.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/max_ascending_subarr_sum.py b/max_ascending_subarr_sum.py new file mode 100644 index 0000000..54c938f --- /dev/null +++ b/max_ascending_subarr_sum.py @@ -0,0 +1,29 @@ +# 1800. Maximum Ascending Subarray Sum + +""" +given an array of positive integers 'nums', return the maximum possible sum +of an ascending subarray in 'nums'. a subarray is defined as a contiguous +sequence of numbers in an array. +""" + + +class Solution(object): + def maxAscendingSum(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + ans, sum = 0, nums[0] + for i in range(len(nums)): + if nums[i] <= nums[i - 1]: + ans = max(ans, sum) + sum = 0 + sum += nums[i] + return max(ans, sum) + + +if __name__ == "__main__": + obj = Solution() + print(obj.maxAscendingSum(nums=[10, 20, 30, 5, 10, 50])) + print(obj.maxAscendingSum(nums=[10, 20, 30, 40, 50])) + print(obj.maxAscendingSum(nums=[12, 17, 15, 13, 10, 11, 12])) |