2 minutes
Leetcode 1800
1800. Maximum Ascending Subarray Sum
The idea of this solution is pretty simple:
- We need two variables,
sum
for the current sum of ascending values andmaximum
for the maximum sum. - Then we loop through
nums
. - Inside the loop, we check whether the current number is greater than the previous number. If so, we can add the number to
sum
. - Else the number is smaller than or equal to the previous number, we can make
maximum
equal to themath.Max(maximum, sum)
and reset sum to the current number. - After we have looped through
nums
we can makemaximum
equal tomath.Max(maximum, sum)
again. - Now we can return
maximum.
The Code:
Same Solution Without math.Max()
Read other posts