Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ba230e7

Browse files
authored
Merge pull request #1270 from 0xff-dev/3487
Add solution and test-cases for problem 3487
2 parents e362002 + 20efae7 commit ba230e7

File tree

3 files changed

+62
-21
lines changed

3 files changed

+62
-21
lines changed

‎leetcode/3401-3500/3487.Maximum-Unique-Subarray-Sum-After-Deletion/README.md‎

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,50 @@
11
# [3487.Maximum Unique Subarray Sum After Deletion][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`.
5+
6+
You are allowed to delete any number of elements from `nums` without making it **empty**. After performing the deletions, select a subarray of `nums` such that:
7+
8+
1. All elements in the subarray are **unique**.
9+
2. The sum of the elements in the subarray is **maximized**.
10+
11+
Return the **maximum sum** of such a subarray.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
16+
Input: nums = [1,2,3,4,5]
17+
18+
Output: 15
19+
20+
Explanation:
21+
22+
Select the entire array without deleting any element to obtain the maximum sum.
23+
```
24+
25+
**Example 2:**
26+
1327
```
28+
Input: nums = [1,1,0,1,1]
29+
30+
Output: 1
31+
32+
Explanation:
1433
15-
## 题意
16-
> ...
34+
Delete the element nums[0] == 1, nums[1] == 1, nums[2] == 0, and nums[3] == 1. Select the entire array [1] to obtain the maximum sum.
35+
```
1736

18-
## 题解
37+
**Example 3:**
1938

20-
### 思路1
21-
> ...
22-
Maximum Unique Subarray Sum After Deletion
23-
```go
2439
```
40+
Input: nums = [1,2,-1,-2,1,0,-1]
41+
42+
Output: 3
2543
44+
Explanation:
45+
46+
Delete the elements nums[2] == -1 and nums[3] == -2, and select the subarray [2, 1] from [1, 2, 1, 0, -1] to obtain the maximum sum.
47+
```
2648

2749
## 结语
2850

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+
import "sort"
4+
5+
func Solution(nums []int) int {
6+
l := len(nums)
7+
sort.Ints(nums)
8+
index := sort.Search(l, func(i int) bool {
9+
return nums[i] >= 0
10+
})
11+
sum := nums[l-1]
12+
if index == l {
13+
return sum
14+
}
15+
pre := sum
16+
for i := l - 2; i >= index; i-- {
17+
if nums[i] == pre {
18+
continue
19+
}
20+
pre = nums[i]
21+
sum += nums[i]
22+
}
23+
return sum
524
}

‎leetcode/3401-3500/3487.Maximum-Unique-Subarray-Sum-After-Deletion/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{1, 2, 3, 4, 5}, 15},
17+
{"TestCase2", []int{1, 1, 0, 1, 1}, 1},
18+
{"TestCase3", []int{1, 2, -1, -2, 1, 0, -1}, 3},
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)

AltStyle によって変換されたページ (->オリジナル) /