Skip to content

Commit 26aabfe

Browse files
authored
Merge pull request #1133 from 0xff-dev/1749
Add solution and test-cases for problem 1749
2 parents ec1ae07 + 69bb8e2 commit 26aabfe

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

leetcode/1701-1800/1749.Maximum-Absolute-Sum-of-Any-Subarray/README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [1749.Maximum Absolute Sum of Any Subarray][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+
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)`.
5+
6+
Return the **maximum** absolute sum of any **(possibly empty)** subarray of `nums`.
7+
8+
Note that `abs(x)` is defined as follows:
9+
10+
- If `x` is a negative integer, then `abx(x) = -x`.
11+
- If `x` is a non-negative integer, then `abs(x) = x`.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
16+
Input: nums = [1,-3,2,3,-4]
17+
Output: 5
18+
Explanation: The subarray [2,3] has absolute sum = abs(2+3) = abs(5) = 5.
1319
```
1420

15-
## 题意
16-
> ...
21+
**Example 2:**
1722

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Maximum Absolute Sum of Any Subarray
23-
```go
2423
```
25-
24+
Input: nums = [2,-5,1,-4,3,-2]
25+
Output: 8
26+
Explanation: The subarray [-5,1,-4] has absolute sum = abs(-5+1-4) = abs(-8) = 8.
27+
```
2628

2729
## 结语
2830

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
ans := 0
5+
_max, _min := 0, 0 //最大的正数,最小的负数
6+
sum := 0
7+
for _, n := range nums {
8+
cur := n
9+
if cur < 0 {
10+
cur = -n
11+
}
12+
ans = max(ans, cur)
13+
sum += n
14+
if sum >= 0 {
15+
cur = sum - _min
16+
_max = max(_max, sum)
17+
} else {
18+
cur = -(sum - _max)
19+
_min = min(_min, sum)
20+
}
21+
ans = max(ans, cur)
22+
}
23+
return ans
524
}

leetcode/1701-1800/1749.Maximum-Absolute-Sum-of-Any-Subarray/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ 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{1, -3, 2, 3, -4}, 5},
17+
{"TestCase2", []int{2, -5, 1, -4, 3, -2}, 8},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)