From 53eba9faf3e888c85b66845087e0faeb94c91d2e Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 18 Dec 2024 09:25:17 +0800 Subject: [PATCH] Add solution and test-cases for problem 2099 --- .../README.md | 37 ++++++++++++------- .../Solution.go | 25 ++++++++++++- .../Solution_test.go | 21 ++++++----- 3 files changed, 58 insertions(+), 25 deletions(-) diff --git a/leetcode/2001-2100/2099.Find-Subsequence-of-Length-K-With-the-Largest-Sum/README.md b/leetcode/2001-2100/2099.Find-Subsequence-of-Length-K-With-the-Largest-Sum/README.md index 2a353b7cf..20b4fed1f 100755 --- a/leetcode/2001-2100/2099.Find-Subsequence-of-Length-K-With-the-Largest-Sum/README.md +++ b/leetcode/2001-2100/2099.Find-Subsequence-of-Length-K-With-the-Largest-Sum/README.md @@ -1,28 +1,39 @@ # [2099.Find Subsequence of Length K With the Largest Sum][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` and an integer `k`. You want to find a **subsequence** of `nums` of length `k` that has the **largest** sum. + +Return **any** such subsequence as an integer array of length `k`. + +A **subsequence** is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [2,1,3,3], k = 2 +Output: [3,3] +Explanation: +The subsequence has the largest sum of 3 + 3 = 6. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Find Subsequence of Length K With the Largest Sum -```go ``` +Input: nums = [-1,-2,3,4], k = 3 +Output: [-1,3,4] +Explanation: +The subsequence has the largest sum of -1 + 3 + 4 = 6. +``` + +**Example 3:** +``` +Input: nums = [3,4,3,3], k = 2 +Output: [3,4] +Explanation: +The subsequence has the largest sum of 3 + 4 = 7. +Another possible subsequence is [4, 3]. +``` ## 结语 diff --git a/leetcode/2001-2100/2099.Find-Subsequence-of-Length-K-With-the-Largest-Sum/Solution.go b/leetcode/2001-2100/2099.Find-Subsequence-of-Length-K-With-the-Largest-Sum/Solution.go index d115ccf5e..6b457574f 100644 --- a/leetcode/2001-2100/2099.Find-Subsequence-of-Length-K-With-the-Largest-Sum/Solution.go +++ b/leetcode/2001-2100/2099.Find-Subsequence-of-Length-K-With-the-Largest-Sum/Solution.go @@ -1,5 +1,26 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(nums []int, k int) []int { + items := make([]int, 0) + for i := range nums { + items = append(items, i) + } + sort.Slice(items, func(i, j int) bool { + if nums[items[i]] == nums[items[j]] { + return i < j + } + return nums[items[i]] > nums[items[j]] + }) + + items = items[:k] + sort.Slice(items, func(i, j int) bool { + return items[i] < items[j] + }) + ans := make([]int, k) + for i := range items { + ans[i] = nums[items[i]] + } + return ans } diff --git a/leetcode/2001-2100/2099.Find-Subsequence-of-Length-K-With-the-Largest-Sum/Solution_test.go b/leetcode/2001-2100/2099.Find-Subsequence-of-Length-K-With-the-Largest-Sum/Solution_test.go index 14ff50eb4..bb6f17ccc 100644 --- a/leetcode/2001-2100/2099.Find-Subsequence-of-Length-K-With-the-Largest-Sum/Solution_test.go +++ b/leetcode/2001-2100/2099.Find-Subsequence-of-Length-K-With-the-Largest-Sum/Solution_test.go @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + nums []int + k int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{2, 1, 3, 3}, 2, []int{3, 3}}, + {"TestCase2", []int{-1, -2, 3, 4}, 3, []int{-1, 3, 4}}, + {"TestCase3", []int{3, 4, 3, 3}, 2, []int{3, 4}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.nums, c.k) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.nums, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }