Skip to content

Commit 1ef5290

Browse files
committed
New Problem Solution -"Maximum Ascending Subarray Sum"
1 parent 55f8340 commit 1ef5290

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ LeetCode
99

1010
| # | Title | Solution | Difficulty |
1111
|---| ----- | -------- | ---------- |
12+
|1800|[Maximum Ascending Subarray Sum](https://leetcode.com/problems/maximum-ascending-subarray-sum/) | [C++](./algorithms/cpp/maximumAscendingSubarraySum/MaximumAscendingSubarraySum.cpp)|Easy|
1213
|1793|[Maximum Score of a Good Subarray](https://leetcode.com/problems/maximum-score-of-a-good-subarray/) | [C++](./algorithms/cpp/maximumScoreOfAGoodSubarray/MaximumScoreOfAGoodSubarray.cpp)|Hard|
1314
|1792|[Maximum Average Pass Ratio](https://leetcode.com/problems/maximum-average-pass-ratio/) | [C++](./algorithms/cpp/maximumAveragePassRatio/MaximumAveragePassRatio.cpp)|Medium|
1415
|1791|[Find Center of Star Graph](https://leetcode.com/problems/find-center-of-star-graph/) | [C++](./algorithms/cpp/findCenterOfStarGraph/FindCenterOfStarGraph.cpp)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Source : https://leetcode.com/problems/maximum-ascending-subarray-sum/
2+
// Author : Hao Chen
3+
// Date : 2021-03-21
4+
5+
/*****************************************************************************************************
6+
*
7+
* Given an array of positive integers nums, return the maximum possible sum of an ascending subarray
8+
* in nums.
9+
*
10+
* A subarray is defined as a contiguous sequence of numbers in an array.
11+
*
12+
* A subarray [numsl, numsl+1, ..., numsr-1, numsr] is ascending if for all i where l <= i < r, numsi
13+
* < numsi+1. Note that a subarray of size 1 is ascending.
14+
*
15+
* Example 1:
16+
*
17+
* Input: nums = [10,20,30,5,10,50]
18+
* Output: 65
19+
* Explanation: [5,10,50] is the ascending subarray with the maximum sum of 65.
20+
*
21+
* Example 2:
22+
*
23+
* Input: nums = [10,20,30,40,50]
24+
* Output: 150
25+
* Explanation: [10,20,30,40,50] is the ascending subarray with the maximum sum of 150.
26+
*
27+
* Example 3:
28+
*
29+
* Input: nums = [12,17,15,13,10,11,12]
30+
* Output: 33
31+
* Explanation: [10,11,12] is the ascending subarray with the maximum sum of 33.
32+
*
33+
* Example 4:
34+
*
35+
* Input: nums = [100,10,1]
36+
* Output: 100
37+
*
38+
* Constraints:
39+
*
40+
* 1 <= nums.length <= 100
41+
* 1 <= nums[i] <= 100
42+
******************************************************************************************************/
43+
44+
class Solution {
45+
public:
46+
int maxAscendingSum(vector<int>& nums) {
47+
int maxSum = nums[0];
48+
int sum = maxSum;
49+
for(int i=1; i<nums.size(); i++) {
50+
if (nums[i] > nums[i-1]) {
51+
sum += nums[i];
52+
}else{
53+
maxSum = maxSum < sum ? sum : maxSum;
54+
sum = nums[i];
55+
}
56+
}
57+
maxSum = maxSum < sum ? sum : maxSum;
58+
return maxSum;
59+
}
60+
};

0 commit comments

Comments
 (0)