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:
func maxAscendingSum(nums []int) int {
maximum := nums[0]
sum := nums[0]
for i := 1; i < len(nums); i++ {
if nums[i] > nums[i-1] {
sum += nums[i]
} else {
maximum, sum =
int(math.Max(float64(maximum), float64(sum))), nums[i]
}
}
maximum = int(math.Max(float64(maximum), float64(sum)))
return maximum
}
Same Solution Without math.Max()
func maxAscendingSum(nums []int) int {
maximum := nums[0]
sum := nums[0]
for i := 1; i < len(nums); i++ {
if nums[i] > nums[i-1] {
sum += nums[i]
} else {
maximum, sum = max(maximum, sum), nums[i]
}
}
maximum = max(maximum, sum)
return maximum
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
Read other posts