Skip to content

feat: add solutions to lc problem: No.2099 #4527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ class Solution {
public int[] maxSubsequence(int[] nums, int k) {
int n = nums.length;
Integer[] idx = new Integer[n];
for (int i = 0; i < n; ++i) {
idx[i] = i;
}
Arrays.setAll(idx, i -> i);
Arrays.sort(idx, (i, j) -> nums[i] - nums[j]);
Arrays.sort(idx, n - k, n);
int[] ans = new int[k];
Expand All @@ -109,20 +107,41 @@ class Solution {
}
```

#### C++

```cpp
#include <ranges>

class Solution {
public:
vector<int> maxSubsequence(vector<int>& nums, int k) {
int n = nums.size();
vector<int> idx(n);
ranges::iota(idx, 0);
ranges::sort(idx, [&](int i, int j) { return nums[i] < nums[j]; });
ranges::sort(idx | views::drop(n - k));
vector<int> ans(k);
for (int i = n - k; i < n; ++i) {
ans[i - (n - k)] = nums[idx[i]];
}
return ans;
}
};
```

#### Go

```go
func maxSubsequence(nums []int, k int) []int {
n := len(nums)
idx := make([]int, n)
idx := slices.Clone(make([]int, len(nums)))
for i := range idx {
idx[i] = i
}
sort.Slice(idx, func(i, j int) bool { return nums[idx[i]] < nums[idx[j]] })
sort.Ints(idx[n-k:])
slices.SortFunc(idx, func(i, j int) int { return nums[i] - nums[j] })
slices.Sort(idx[len(idx)-k:])
ans := make([]int, k)
for i := n - k; i < n; i++ {
ans[i-(n-k)] = nums[idx[i]]
for i := range ans {
ans[i] = nums[idx[len(idx)-k+i]]
}
return ans
}
Expand All @@ -142,6 +161,28 @@ function maxSubsequence(nums: number[], k: number): number[] {
}
```

#### Rust

```rust
impl Solution {
pub fn max_subsequence(nums: Vec<i32>, k: i32) -> Vec<i32> {
let n = nums.len();
let k = k as usize;
let mut idx: Vec<usize> = (0..n).collect();

idx.sort_by_key(|&i| nums[i]);
idx[n - k..].sort();

let mut ans = Vec::with_capacity(k);
for i in n - k..n {
ans.push(nums[idx[i]]);
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ class Solution {
public int[] maxSubsequence(int[] nums, int k) {
int n = nums.length;
Integer[] idx = new Integer[n];
for (int i = 0; i < n; ++i) {
idx[i] = i;
}
Arrays.setAll(idx, i -> i);
Arrays.sort(idx, (i, j) -> nums[i] - nums[j]);
Arrays.sort(idx, n - k, n);
int[] ans = new int[k];
Expand All @@ -110,20 +108,41 @@ class Solution {
}
```

#### C++

```cpp
#include <ranges>

class Solution {
public:
vector<int> maxSubsequence(vector<int>& nums, int k) {
int n = nums.size();
vector<int> idx(n);
ranges::iota(idx, 0);
ranges::sort(idx, [&](int i, int j) { return nums[i] < nums[j]; });
ranges::sort(idx | views::drop(n - k));
vector<int> ans(k);
for (int i = n - k; i < n; ++i) {
ans[i - (n - k)] = nums[idx[i]];
}
return ans;
}
};
```
#### Go
```go
func maxSubsequence(nums []int, k int) []int {
n := len(nums)
idx := make([]int, n)
idx := slices.Clone(make([]int, len(nums)))
for i := range idx {
idx[i] = i
}
sort.Slice(idx, func(i, j int) bool { return nums[idx[i]] < nums[idx[j]] })
sort.Ints(idx[n-k:])
slices.SortFunc(idx, func(i, j int) int { return nums[i] - nums[j] })
slices.Sort(idx[len(idx)-k:])
ans := make([]int, k)
for i := n - k; i < n; i++ {
ans[i-(n-k)] = nums[idx[i]]
for i := range ans {
ans[i] = nums[idx[len(idx)-k+i]]
}
return ans
}
Expand All @@ -143,6 +162,28 @@ function maxSubsequence(nums: number[], k: number): number[] {
}
```

#### Rust

```rust
impl Solution {
pub fn max_subsequence(nums: Vec<i32>, k: i32) -> Vec<i32> {
let n = nums.len();
let k = k as usize;
let mut idx: Vec<usize> = (0..n).collect();

idx.sort_by_key(|&i| nums[i]);
idx[n - k..].sort();

let mut ans = Vec::with_capacity(k);
for i in n - k..n {
ans.push(nums[idx[i]]);
}

ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include <ranges>

class Solution {
public:
vector<int> maxSubsequence(vector<int>& nums, int k) {
int n = nums.size();
vector<int> idx(n);
for (int i = 0; i < n; ++i) {
idx[i] = i;
}
sort(idx.begin(), idx.end(), [&](int i, int j) { return nums[i] < nums[j]; });
sort(idx.begin() + (n - k), idx.end());
ranges::iota(idx, 0);
ranges::sort(idx, [&](int i, int j) { return nums[i] < nums[j]; });
ranges::sort(idx | views::drop(n - k));
vector<int> ans(k);
for (int i = n - k; i < n; ++i) {
ans[i - (n - k)] = nums[idx[i]];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
func maxSubsequence(nums []int, k int) []int {
n := len(nums)
idx := make([]int, n)
idx := slices.Clone(make([]int, len(nums)))
for i := range idx {
idx[i] = i
}
sort.Slice(idx, func(i, j int) bool { return nums[idx[i]] < nums[idx[j]] })
sort.Ints(idx[n-k:])
slices.SortFunc(idx, func(i, j int) int { return nums[i] - nums[j] })
slices.Sort(idx[len(idx)-k:])
ans := make([]int, k)
for i := n - k; i < n; i++ {
ans[i-(n-k)] = nums[idx[i]]
for i := range ans {
ans[i] = nums[idx[len(idx)-k+i]]
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ class Solution {
public int[] maxSubsequence(int[] nums, int k) {
int n = nums.length;
Integer[] idx = new Integer[n];
for (int i = 0; i < n; ++i) {
idx[i] = i;
}
Arrays.setAll(idx, i -> i);
Arrays.sort(idx, (i, j) -> nums[i] - nums[j]);
Arrays.sort(idx, n - k, n);
int[] ans = new int[k];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
impl Solution {
pub fn max_subsequence(nums: Vec<i32>, k: i32) -> Vec<i32> {
let n = nums.len();
let k = k as usize;
let mut idx: Vec<usize> = (0..n).collect();

idx.sort_by_key(|&i| nums[i]);
idx[n - k..].sort();

let mut ans = Vec::with_capacity(k);
for i in n - k..n {
ans.push(nums[idx[i]]);
}

ans
}
}