diff --git a/leetcode/1701-1800/1749.Maximum-Absolute-Sum-of-Any-Subarray/README.md b/leetcode/1701-1800/1749.Maximum-Absolute-Sum-of-Any-Subarray/README.md index c701fd665..608e9e72f 100755 --- a/leetcode/1701-1800/1749.Maximum-Absolute-Sum-of-Any-Subarray/README.md +++ b/leetcode/1701-1800/1749.Maximum-Absolute-Sum-of-Any-Subarray/README.md @@ -1,28 +1,30 @@ # [1749.Maximum Absolute Sum of Any Subarray][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 +You are given an integer array nums. The **absolute sum** of a subarray `[numsl, numsl+1, ..., numsr-1, numsr]` is `abs(numsl + numsl+1 + ... + numsr-1 + numsr)`. + +Return the **maximum** absolute sum of any **(possibly empty)** subarray of `nums`. + +Note that `abs(x)` is defined as follows: + +- If `x` is a negative integer, then `abx(x) = -x`. +- If `x` is a non-negative integer, then `abs(x) = x`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [1,-3,2,3,-4] +Output: 5 +Explanation: The subarray [2,3] has absolute sum = abs(2+3) = abs(5) = 5. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Maximum Absolute Sum of Any Subarray -```go ``` - +Input: nums = [2,-5,1,-4,3,-2] +Output: 8 +Explanation: The subarray [-5,1,-4] has absolute sum = abs(-5+1-4) = abs(-8) = 8. +``` ## 结语 diff --git a/leetcode/1701-1800/1749.Maximum-Absolute-Sum-of-Any-Subarray/Solution.go b/leetcode/1701-1800/1749.Maximum-Absolute-Sum-of-Any-Subarray/Solution.go index d115ccf5e..49f4d11f5 100644 --- a/leetcode/1701-1800/1749.Maximum-Absolute-Sum-of-Any-Subarray/Solution.go +++ b/leetcode/1701-1800/1749.Maximum-Absolute-Sum-of-Any-Subarray/Solution.go @@ -1,5 +1,24 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) int { + ans := 0 + _max, _min := 0, 0 //最大的正数,最小的负数 + sum := 0 + for _, n := range nums { + cur := n + if cur < 0 { + cur = -n + } + ans = max(ans, cur) + sum += n + if sum >= 0 { + cur = sum - _min + _max = max(_max, sum) + } else { + cur = -(sum - _max) + _min = min(_min, sum) + } + ans = max(ans, cur) + } + return ans } diff --git a/leetcode/1701-1800/1749.Maximum-Absolute-Sum-of-Any-Subarray/Solution_test.go b/leetcode/1701-1800/1749.Maximum-Absolute-Sum-of-Any-Subarray/Solution_test.go index 14ff50eb4..3e14fac92 100644 --- a/leetcode/1701-1800/1749.Maximum-Absolute-Sum-of-Any-Subarray/Solution_test.go +++ b/leetcode/1701-1800/1749.Maximum-Absolute-Sum-of-Any-Subarray/Solution_test.go @@ -10,12 +10,11 @@ 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{1, -3, 2, 3, -4}, 5}, + {"TestCase2", []int{2, -5, 1, -4, 3, -2}, 8}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }