From 20efae7bcd5168c93b0f8708bfb6c958bfa7e8b7 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Fri, 25 Jul 2025 08:56:25 +0800 Subject: [PATCH] Add solution and test-cases for problem 3487 --- .../README.md | 46 ++++++++++++++----- .../Solution.go | 23 +++++++++- .../Solution_test.go | 14 +++--- 3 files changed, 62 insertions(+), 21 deletions(-) diff --git a/leetcode/3401-3500/3487.Maximum-Unique-Subarray-Sum-After-Deletion/README.md b/leetcode/3401-3500/3487.Maximum-Unique-Subarray-Sum-After-Deletion/README.md index eca18aba2..523c2cd7a 100755 --- a/leetcode/3401-3500/3487.Maximum-Unique-Subarray-Sum-After-Deletion/README.md +++ b/leetcode/3401-3500/3487.Maximum-Unique-Subarray-Sum-After-Deletion/README.md @@ -1,28 +1,50 @@ # [3487.Maximum Unique Subarray Sum After Deletion][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`. + +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: + +1. All elements in the subarray are **unique**. +2. The sum of the elements in the subarray is **maximized**. + +Return the **maximum sum** of such a subarray. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [1,2,3,4,5] + +Output: 15 + +Explanation: + +Select the entire array without deleting any element to obtain the maximum sum. +``` + +**Example 2:** + ``` +Input: nums = [1,1,0,1,1] + +Output: 1 + +Explanation: -## 题意 -> ... +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. +``` -## 题解 +**Example 3:** -### 思路1 -> ... -Maximum Unique Subarray Sum After Deletion -```go ``` +Input: nums = [1,2,-1,-2,1,0,-1] + +Output: 3 +Explanation: + +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. +``` ## 结语 diff --git a/leetcode/3401-3500/3487.Maximum-Unique-Subarray-Sum-After-Deletion/Solution.go b/leetcode/3401-3500/3487.Maximum-Unique-Subarray-Sum-After-Deletion/Solution.go index d115ccf5e..c76f7a37f 100644 --- a/leetcode/3401-3500/3487.Maximum-Unique-Subarray-Sum-After-Deletion/Solution.go +++ b/leetcode/3401-3500/3487.Maximum-Unique-Subarray-Sum-After-Deletion/Solution.go @@ -1,5 +1,24 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(nums []int) int { + l := len(nums) + sort.Ints(nums) + index := sort.Search(l, func(i int) bool { + return nums[i] >= 0 + }) + sum := nums[l-1] + if index == l { + return sum + } + pre := sum + for i := l - 2; i >= index; i-- { + if nums[i] == pre { + continue + } + pre = nums[i] + sum += nums[i] + } + return sum } diff --git a/leetcode/3401-3500/3487.Maximum-Unique-Subarray-Sum-After-Deletion/Solution_test.go b/leetcode/3401-3500/3487.Maximum-Unique-Subarray-Sum-After-Deletion/Solution_test.go index 14ff50eb4..e81bf4c4f 100644 --- a/leetcode/3401-3500/3487.Maximum-Unique-Subarray-Sum-After-Deletion/Solution_test.go +++ b/leetcode/3401-3500/3487.Maximum-Unique-Subarray-Sum-After-Deletion/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{1, 2, 3, 4, 5}, 15}, + {"TestCase2", []int{1, 1, 0, 1, 1}, 1}, + {"TestCase3", []int{1, 2, -1, -2, 1, 0, -1}, 3}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }