From a6e12b8f05c27a9b9f3d0668574361a5eb42cbf1 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Tue, 4 Feb 2025 09:31:36 +0800 Subject: [PATCH] Add solution and test-cases for problem 1800 --- .../README.md | 33 +++++++++++-------- .../Solution.go | 17 ++++++++-- .../Solution_test.go | 14 ++++---- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/leetcode/1701-1800/1800.Maximum-Ascending-Subarray-Sum/README.md b/leetcode/1701-1800/1800.Maximum-Ascending-Subarray-Sum/README.md index af577631c..0c0777378 100755 --- a/leetcode/1701-1800/1800.Maximum-Ascending-Subarray-Sum/README.md +++ b/leetcode/1701-1800/1800.Maximum-Ascending-Subarray-Sum/README.md @@ -1,28 +1,35 @@ # [1800.Maximum Ascending Subarray Sum][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +Given an array of positive integers `nums`, return the maximum possible sum of an **ascending** subarray in `nums`. + +A subarray is defined as a contiguous sequence of numbers in an array. + +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**. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +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:** -### 思路1 -> ... -Maximum Ascending Subarray Sum -```go ``` +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. +``` ## 结语 diff --git a/leetcode/1701-1800/1800.Maximum-Ascending-Subarray-Sum/Solution.go b/leetcode/1701-1800/1800.Maximum-Ascending-Subarray-Sum/Solution.go index d115ccf5e..373aa4afc 100644 --- a/leetcode/1701-1800/1800.Maximum-Ascending-Subarray-Sum/Solution.go +++ b/leetcode/1701-1800/1800.Maximum-Ascending-Subarray-Sum/Solution.go @@ -1,5 +1,18 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) int { + sum := 0 + pre := 0 + ans := 0 + for _, n := range nums { + if n > pre { + sum += n + pre = n + continue + } + ans = max(ans, sum) + sum = n + pre = n + } + return max(ans, sum) } diff --git a/leetcode/1701-1800/1800.Maximum-Ascending-Subarray-Sum/Solution_test.go b/leetcode/1701-1800/1800.Maximum-Ascending-Subarray-Sum/Solution_test.go index 14ff50eb4..e9ff0ee1a 100644 --- a/leetcode/1701-1800/1800.Maximum-Ascending-Subarray-Sum/Solution_test.go +++ b/leetcode/1701-1800/1800.Maximum-Ascending-Subarray-Sum/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{10, 20, 30, 5, 10, 50}, 65}, + {"TestCase2", []int{10, 20, 30, 40, 50}, 150}, + {"TestCase3", []int{12, 17, 15, 13, 10, 11, 12}, 33}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }