Skip to content

Commit 7888bcd

Browse files
committed
feat: add solutions to lc problem: No.2099
No.2099.Find Subsequence of Length K With the Largest Sum
1 parent 8c4ea1f commit 7888bcd

File tree

6 files changed

+128
-32
lines changed

6 files changed

+128
-32
lines changed

solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README.md

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ class Solution {
9595
public int[] maxSubsequence(int[] nums, int k) {
9696
int n = nums.length;
9797
Integer[] idx = new Integer[n];
98-
for (int i = 0; i < n; ++i) {
99-
idx[i] = i;
100-
}
98+
Arrays.setAll(idx, i -> i);
10199
Arrays.sort(idx, (i, j) -> nums[i] - nums[j]);
102100
Arrays.sort(idx, n - k, n);
103101
int[] ans = new int[k];
@@ -109,20 +107,41 @@ class Solution {
109107
}
110108
```
111109

110+
#### C++
111+
112+
```cpp
113+
#include <ranges>
114+
115+
class Solution {
116+
public:
117+
vector<int> maxSubsequence(vector<int>& nums, int k) {
118+
int n = nums.size();
119+
vector<int> idx(n);
120+
ranges::iota(idx, 0);
121+
ranges::sort(idx, [&](int i, int j) { return nums[i] < nums[j]; });
122+
ranges::sort(idx | views::drop(n - k));
123+
vector<int> ans(k);
124+
for (int i = n - k; i < n; ++i) {
125+
ans[i - (n - k)] = nums[idx[i]];
126+
}
127+
return ans;
128+
}
129+
};
130+
```
131+
112132
#### Go
113133
114134
```go
115135
func maxSubsequence(nums []int, k int) []int {
116-
n := len(nums)
117-
idx := make([]int, n)
136+
idx := slices.Clone(make([]int, len(nums)))
118137
for i := range idx {
119138
idx[i] = i
120139
}
121-
sort.Slice(idx, func(i, j int) bool { return nums[idx[i]] < nums[idx[j]] })
122-
sort.Ints(idx[n-k:])
140+
slices.SortFunc(idx, func(i, j int) int { return nums[i] - nums[j] })
141+
slices.Sort(idx[len(idx)-k:])
123142
ans := make([]int, k)
124-
for i := n - k; i < n; i++ {
125-
ans[i-(n-k)] = nums[idx[i]]
143+
for i := range ans {
144+
ans[i] = nums[idx[len(idx)-k+i]]
126145
}
127146
return ans
128147
}
@@ -142,6 +161,28 @@ function maxSubsequence(nums: number[], k: number): number[] {
142161
}
143162
```
144163

164+
#### Rust
165+
166+
```rust
167+
impl Solution {
168+
pub fn max_subsequence(nums: Vec<i32>, k: i32) -> Vec<i32> {
169+
let n = nums.len();
170+
let k = k as usize;
171+
let mut idx: Vec<usize> = (0..n).collect();
172+
173+
idx.sort_by_key(|&i| nums[i]);
174+
idx[n - k..].sort();
175+
176+
let mut ans = Vec::with_capacity(k);
177+
for i in n - k..n {
178+
ans.push(nums[idx[i]]);
179+
}
180+
181+
ans
182+
}
183+
}
184+
```
185+
145186
<!-- tabs:end -->
146187

147188
<!-- solution:end -->

solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README_EN.md

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ class Solution {
9696
public int[] maxSubsequence(int[] nums, int k) {
9797
int n = nums.length;
9898
Integer[] idx = new Integer[n];
99-
for (int i = 0; i < n; ++i) {
100-
idx[i] = i;
101-
}
99+
Arrays.setAll(idx, i -> i);
102100
Arrays.sort(idx, (i, j) -> nums[i] - nums[j]);
103101
Arrays.sort(idx, n - k, n);
104102
int[] ans = new int[k];
@@ -110,20 +108,41 @@ class Solution {
110108
}
111109
```
112110

111+
#### C++
112+
113+
```cpp
114+
#include <ranges>
115+
116+
class Solution {
117+
public:
118+
vector<int> maxSubsequence(vector<int>& nums, int k) {
119+
int n = nums.size();
120+
vector<int> idx(n);
121+
ranges::iota(idx, 0);
122+
ranges::sort(idx, [&](int i, int j) { return nums[i] < nums[j]; });
123+
ranges::sort(idx | views::drop(n - k));
124+
vector<int> ans(k);
125+
for (int i = n - k; i < n; ++i) {
126+
ans[i - (n - k)] = nums[idx[i]];
127+
}
128+
return ans;
129+
}
130+
};
131+
```
132+
113133
#### Go
114134
115135
```go
116136
func maxSubsequence(nums []int, k int) []int {
117-
n := len(nums)
118-
idx := make([]int, n)
137+
idx := slices.Clone(make([]int, len(nums)))
119138
for i := range idx {
120139
idx[i] = i
121140
}
122-
sort.Slice(idx, func(i, j int) bool { return nums[idx[i]] < nums[idx[j]] })
123-
sort.Ints(idx[n-k:])
141+
slices.SortFunc(idx, func(i, j int) int { return nums[i] - nums[j] })
142+
slices.Sort(idx[len(idx)-k:])
124143
ans := make([]int, k)
125-
for i := n - k; i < n; i++ {
126-
ans[i-(n-k)] = nums[idx[i]]
144+
for i := range ans {
145+
ans[i] = nums[idx[len(idx)-k+i]]
127146
}
128147
return ans
129148
}
@@ -143,6 +162,28 @@ function maxSubsequence(nums: number[], k: number): number[] {
143162
}
144163
```
145164

165+
#### Rust
166+
167+
```rust
168+
impl Solution {
169+
pub fn max_subsequence(nums: Vec<i32>, k: i32) -> Vec<i32> {
170+
let n = nums.len();
171+
let k = k as usize;
172+
let mut idx: Vec<usize> = (0..n).collect();
173+
174+
idx.sort_by_key(|&i| nums[i]);
175+
idx[n - k..].sort();
176+
177+
let mut ans = Vec::with_capacity(k);
178+
for i in n - k..n {
179+
ans.push(nums[idx[i]]);
180+
}
181+
182+
ans
183+
}
184+
}
185+
```
186+
146187
<!-- tabs:end -->
147188

148189
<!-- solution:end -->

solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
#include <ranges>
2+
13
class Solution {
24
public:
35
vector<int> maxSubsequence(vector<int>& nums, int k) {
46
int n = nums.size();
57
vector<int> idx(n);
6-
for (int i = 0; i < n; ++i) {
7-
idx[i] = i;
8-
}
9-
sort(idx.begin(), idx.end(), [&](int i, int j) { return nums[i] < nums[j]; });
10-
sort(idx.begin() + (n - k), idx.end());
8+
ranges::iota(idx, 0);
9+
ranges::sort(idx, [&](int i, int j) { return nums[i] < nums[j]; });
10+
ranges::sort(idx | views::drop(n - k));
1111
vector<int> ans(k);
1212
for (int i = n - k; i < n; ++i) {
1313
ans[i - (n - k)] = nums[idx[i]];
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
func maxSubsequence(nums []int, k int) []int {
2-
n := len(nums)
3-
idx := make([]int, n)
2+
idx := slices.Clone(make([]int, len(nums)))
43
for i := range idx {
54
idx[i] = i
65
}
7-
sort.Slice(idx, func(i, j int) bool { return nums[idx[i]] < nums[idx[j]] })
8-
sort.Ints(idx[n-k:])
6+
slices.SortFunc(idx, func(i, j int) int { return nums[i] - nums[j] })
7+
slices.Sort(idx[len(idx)-k:])
98
ans := make([]int, k)
10-
for i := n - k; i < n; i++ {
11-
ans[i-(n-k)] = nums[idx[i]]
9+
for i := range ans {
10+
ans[i] = nums[idx[len(idx)-k+i]]
1211
}
1312
return ans
1413
}

solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ class Solution {
22
public int[] maxSubsequence(int[] nums, int k) {
33
int n = nums.length;
44
Integer[] idx = new Integer[n];
5-
for (int i = 0; i < n; ++i) {
6-
idx[i] = i;
7-
}
5+
Arrays.setAll(idx, i -> i);
86
Arrays.sort(idx, (i, j) -> nums[i] - nums[j]);
97
Arrays.sort(idx, n - k, n);
108
int[] ans = new int[k];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
impl Solution {
2+
pub fn max_subsequence(nums: Vec<i32>, k: i32) -> Vec<i32> {
3+
let n = nums.len();
4+
let k = k as usize;
5+
let mut idx: Vec<usize> = (0..n).collect();
6+
7+
idx.sort_by_key(|&i| nums[i]);
8+
idx[n - k..].sort();
9+
10+
let mut ans = Vec::with_capacity(k);
11+
for i in n - k..n {
12+
ans.push(nums[idx[i]]);
13+
}
14+
15+
ans
16+
}
17+
}

0 commit comments

Comments
 (0)