Skip to content

Commit d892e88

Browse files
authored
Merge pull request #1112 from 0xff-dev/1800
Add solution and test-cases for problem 1800
2 parents 31d140b + a6e12b8 commit d892e88

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed

leetcode/1701-1800/1800.Maximum-Ascending-Subarray-Sum/README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
# [1800.Maximum Ascending Subarray Sum][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given an array of positive integers `nums`, return the maximum possible sum of an **ascending** subarray in `nums`.
5+
6+
A subarray is defined as a contiguous sequence of numbers in an array.
7+
8+
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**.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: nums = [10,20,30,5,10,50]
14+
Output: 65
15+
Explanation: [5,10,50] is the ascending subarray with the maximum sum of 65.
1316
```
1417

15-
## 题意
16-
> ...
17-
18-
## 题解
18+
**Example 2:**
1919

20-
### 思路1
21-
> ...
22-
Maximum Ascending Subarray Sum
23-
```go
2420
```
21+
Input: nums = [10,20,30,40,50]
22+
Output: 150
23+
Explanation: [10,20,30,40,50] is the ascending subarray with the maximum sum of 150.
24+
```
25+
26+
**Example 3:**
2527

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+
```
2633

2734
## 结语
2835

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
sum := 0
5+
pre := 0
6+
ans := 0
7+
for _, n := range nums {
8+
if n > pre {
9+
sum += n
10+
pre = n
11+
continue
12+
}
13+
ans = max(ans, sum)
14+
sum = n
15+
pre = n
16+
}
17+
return max(ans, sum)
518
}

leetcode/1701-1800/1800.Maximum-Ascending-Subarray-Sum/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{10, 20, 30, 5, 10, 50}, 65},
17+
{"TestCase2", []int{10, 20, 30, 40, 50}, 150},
18+
{"TestCase3", []int{12, 17, 15, 13, 10, 11, 12}, 33},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)