1800. Maximum Ascending Subarray Sum LeetCode Solution

In this guide, you will get 1800. Maximum Ascending Subarray Sum LeetCode Solution with the best time and space complexity. The solution to Maximum Ascending Subarray Sum problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Ascending Subarray Sum solution in C++
  4. Maximum Ascending Subarray Sum solution in Java
  5. Maximum Ascending Subarray Sum solution in Python
  6. Additional Resources
1800. Maximum Ascending Subarray Sum LeetCode Solution image

Problem Statement of Maximum Ascending Subarray Sum

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 1:

Input: nums = [10,20,30,5,10,50]
Output: 65
Explanation: [5,10,50] is the ascending subarray with the maximum sum of 65.

Example 2:

Input: nums = [10,20,30,40,50]
Output: 150
Explanation: [10,20,30,40,50] is the ascending subarray with the maximum sum of 150.

Example 3:

Input: nums = [12,17,15,13,10,11,12]
Output: 33
Explanation: [10,11,12] is the ascending subarray with the maximum sum of 33.

Constraints:

1 <= nums.length <= 100
1 <= nums[i] <= 100

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1800. Maximum Ascending Subarray Sum LeetCode Solution in C++

class Solution {
 public:
  int maxAscendingSum(vector<int>& nums) {
    int ans = 0;
    int sum = nums[0];

    for (int i = 1; i < nums.size(); ++i)
      if (nums[i] > nums[i - 1]) {
        sum += nums[i];
      } else {
        ans = max(ans, sum);
        sum = nums[i];
      }

    return max(ans, sum);
  }
};
/* code provided by PROGIEZ */

1800. Maximum Ascending Subarray Sum LeetCode Solution in Java

class Solution {
  public int maxAscendingSum(int[] nums) {
    int ans = 0;
    int sum = nums[0];

    for (int i = 1; i < nums.length; ++i)
      if (nums[i] > nums[i - 1]) {
        sum += nums[i];
      } else {
        ans = Math.max(ans, sum);
        sum = nums[i];
      }

    return Math.max(ans, sum);
  }
}
// code provided by PROGIEZ

1800. Maximum Ascending Subarray Sum LeetCode Solution in Python

class Solution:
  def maxAscendingSum(self, nums: list[int]) -> int:
    ans = 0
    sum = nums[0]

    for i in range(1, len(nums)):
      if nums[i] > nums[i - 1]:
        sum += nums[i]
      else:
        ans = max(ans, sum)
        sum = nums[i]

    return max(ans, sum)
# code by PROGIEZ

Additional Resources

See also  2487. Remove Nodes From Linked List LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.