diff --git a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README.md b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README.md index a0c24322cf7ec..60a570609910a 100644 --- a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README.md +++ b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README.md @@ -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]; @@ -109,20 +107,41 @@ class Solution { } ``` +#### C++ + +```cpp +#include + +class Solution { +public: + vector maxSubsequence(vector& nums, int k) { + int n = nums.size(); + vector 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 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 } @@ -142,6 +161,28 @@ function maxSubsequence(nums: number[], k: number): number[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn max_subsequence(nums: Vec, k: i32) -> Vec { + let n = nums.len(); + let k = k as usize; + let mut idx: Vec = (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 + } +} +``` + diff --git a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README_EN.md b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README_EN.md index e8465c8bb9729..fe9496ca82787 100644 --- a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README_EN.md +++ b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/README_EN.md @@ -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]; @@ -110,20 +108,41 @@ class Solution { } ``` +#### C++ + +```cpp +#include + +class Solution { +public: + vector maxSubsequence(vector& nums, int k) { + int n = nums.size(); + vector 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 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 } @@ -143,6 +162,28 @@ function maxSubsequence(nums: number[], k: number): number[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn max_subsequence(nums: Vec, k: i32) -> Vec { + let n = nums.len(); + let k = k as usize; + let mut idx: Vec = (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 + } +} +``` + diff --git a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.cpp b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.cpp index 60350b1b94b0a..544a813a15678 100644 --- a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.cpp +++ b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.cpp @@ -1,13 +1,13 @@ +#include + class Solution { public: vector maxSubsequence(vector& nums, int k) { int n = nums.size(); vector 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 ans(k); for (int i = n - k; i < n; ++i) { ans[i - (n - k)] = nums[idx[i]]; diff --git a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.go b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.go index 6dac3c53afae9..4bbfb3ffffcd1 100644 --- a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.go +++ b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.go @@ -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 } \ No newline at end of file diff --git a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.java b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.java index c4286fe13513d..ec70e12c950d8 100644 --- a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.java +++ b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.java @@ -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]; diff --git a/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.rs b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.rs new file mode 100644 index 0000000000000..44c42f409d33d --- /dev/null +++ b/solution/2000-2099/2099.Find Subsequence of Length K With the Largest Sum/Solution.rs @@ -0,0 +1,17 @@ +impl Solution { + pub fn max_subsequence(nums: Vec, k: i32) -> Vec { + let n = nums.len(); + let k = k as usize; + let mut idx: Vec = (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 + } +}