Skip to content

Commit ed6bbd5

Browse files
authored
Merge pull request #1067 from 0xff-dev/2099
Add solution and test-cases for problem 2099
2 parents 798585e + 53eba9f commit ed6bbd5

File tree

3 files changed

+58
-25
lines changed

3 files changed

+58
-25
lines changed

leetcode/2001-2100/2099.Find-Subsequence-of-Length-K-With-the-Largest-Sum/README.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
# [2099.Find Subsequence of Length K With the Largest Sum][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` and an integer `k`. You want to find a **subsequence** of `nums` of length `k` that has the **largest** sum.
5+
6+
Return **any** such subsequence as an integer array of length `k`.
7+
8+
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.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: nums = [2,1,3,3], k = 2
14+
Output: [3,3]
15+
Explanation:
16+
The subsequence has the largest sum of 3 + 3 = 6.
1317
```
1418

15-
## 题意
16-
> ...
17-
18-
## 题解
19+
**Example 2:**
1920

20-
### 思路1
21-
> ...
22-
Find Subsequence of Length K With the Largest Sum
23-
```go
2421
```
22+
Input: nums = [-1,-2,3,4], k = 3
23+
Output: [-1,3,4]
24+
Explanation:
25+
The subsequence has the largest sum of -1 + 3 + 4 = 6.
26+
```
27+
28+
**Example 3:**
2529

30+
```
31+
Input: nums = [3,4,3,3], k = 2
32+
Output: [3,4]
33+
Explanation:
34+
The subsequence has the largest sum of 3 + 4 = 7.
35+
Another possible subsequence is [4, 3].
36+
```
2637

2738
## 结语
2839

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(nums []int, k int) []int {
6+
items := make([]int, 0)
7+
for i := range nums {
8+
items = append(items, i)
9+
}
10+
sort.Slice(items, func(i, j int) bool {
11+
if nums[items[i]] == nums[items[j]] {
12+
return i < j
13+
}
14+
return nums[items[i]] > nums[items[j]]
15+
})
16+
17+
items = items[:k]
18+
sort.Slice(items, func(i, j int) bool {
19+
return items[i] < items[j]
20+
})
21+
ans := make([]int, k)
22+
for i := range items {
23+
ans[i] = nums[items[i]]
24+
}
25+
return ans
526
}

leetcode/2001-2100/2099.Find-Subsequence-of-Length-K-With-the-Largest-Sum/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
nums []int
14+
k int
15+
expect []int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{2, 1, 3, 3}, 2, []int{3, 3}},
18+
{"TestCase2", []int{-1, -2, 3, 4}, 3, []int{-1, 3, 4}},
19+
{"TestCase3", []int{3, 4, 3, 3}, 2, []int{3, 4}},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.nums, c.k)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.nums, c.k)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)