Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading