Skip to content

Commit 7b81795

Browse files
committed
feat: solve No.1800
1 parent 352398b commit 7b81795

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# 1800. Maximum Ascending Subarray Sum
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Array.
5+
- Similar Questions: Find Good Days to Rob the Bank, Maximum Number of Books You Can Take, Count Strictly Increasing Subarrays.
6+
7+
## Problem
8+
9+
Given an array of positive integers `nums`, return the **maximum possible sum of an **ascending** subarray in **`nums`.
10+
11+
A subarray is defined as a contiguous sequence of numbers in an array.
12+
13+
A subarray `[numsl, numsl+1, ..., numsr-1, numsr]` is **ascending** if for all `i` where `l <= i < r`, `numsi < numsi+1`. Note that a subarray of size `1` is **ascending**.
14+
15+
 
16+
Example 1:
17+
18+
```
19+
Input: nums = [10,20,30,5,10,50]
20+
Output: 65
21+
Explanation: [5,10,50] is the ascending subarray with the maximum sum of 65.
22+
```
23+
24+
Example 2:
25+
26+
```
27+
Input: nums = [10,20,30,40,50]
28+
Output: 150
29+
Explanation: [10,20,30,40,50] is the ascending subarray with the maximum sum of 150.
30+
```
31+
32+
Example 3:
33+
34+
```
35+
Input: nums = [12,17,15,13,10,11,12]
36+
Output: 33
37+
Explanation: [10,11,12] is the ascending subarray with the maximum sum of 33.
38+
```
39+
40+
 
41+
**Constraints:**
42+
43+
44+
45+
- `1 <= nums.length <= 100`
46+
47+
- `1 <= nums[i] <= 100`
48+
49+
50+
51+
## Solution
52+
53+
```javascript
54+
/**
55+
* @param {number[]} nums
56+
* @return {number}
57+
*/
58+
var maxAscendingSum = function(nums) {
59+
var maxSum = nums[0];
60+
var currSum = nums[0];
61+
for (var i = 1; i < nums.length; i++) {
62+
if (nums[i - 1] < nums[i]) {
63+
currSum += nums[i];
64+
} else {
65+
maxSum = Math.max(maxSum, currSum);
66+
currSum = nums[i];
67+
}
68+
}
69+
return Math.max(maxSum, currSum);
70+
};
71+
```
72+
73+
**Explain:**
74+
75+
nope.
76+
77+
**Complexity:**
78+
79+
* Time complexity : O(n).
80+
* Space complexity : O(1).

0 commit comments

Comments
 (0)