Skip to content

Commit 7bafa43

Browse files
committed
feat: add solutions to lc problem: No.2099
No.2099.Find Subsequence of Length K With the Largest Sum
1 parent c7c0902 commit 7bafa43

File tree

6 files changed

+181
-2
lines changed

6 files changed

+181
-2
lines changed

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

+66-1
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,87 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
两次排序。
56+
57+
先按照数字大小**对下标进行排序**,然后取出倒数 k 个下标(对应的数字是数组中前 k 个数字),对下标进行排序。最后将排序后的下标依次映射成数字,得到结果数组。
58+
5559
<!-- tabs:start -->
5660

5761
### **Python3**
5862

5963
<!-- 这里可写当前语言的特殊实现逻辑 -->
6064

6165
```python
62-
66+
class Solution:
67+
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
68+
idx = list(range(len(nums)))
69+
idx.sort(key=lambda i: nums[i])
70+
return [nums[i] for i in sorted(idx[-k:])]
6371
```
6472

6573
### **Java**
6674

6775
<!-- 这里可写当前语言的特殊实现逻辑 -->
6876

6977
```java
78+
class Solution {
79+
public int[] maxSubsequence(int[] nums, int k) {
80+
int[] ans = new int[k];
81+
List<Integer> idx = new ArrayList<>();
82+
int n = nums.length;
83+
for (int i = 0; i < n; ++i) {
84+
idx.add(i);
85+
}
86+
idx.sort(Comparator.comparingInt(i -> -nums[i]));
87+
int[] t = new int[k];
88+
for (int i = 0; i < k; ++i) {
89+
t[i] = idx.get(i);
90+
}
91+
Arrays.sort(t);
92+
for (int i = 0; i < k; ++i) {
93+
ans[i] = nums[t[i]];
94+
}
95+
return ans;
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
vector<int> maxSubsequence(vector<int>& nums, int k) {
106+
int n = nums.size();
107+
vector<pair<int, int>> vals;
108+
for (int i = 0; i < n; ++i) vals.push_back({i, nums[i]});
109+
sort(vals.begin(), vals.end(), [&](auto x1, auto x2) {
110+
return x1.second > x2.second;
111+
});
112+
sort(vals.begin(), vals.begin() + k);
113+
vector<int> ans;
114+
for (int i = 0; i < k; ++i) ans.push_back(vals[i].second);
115+
return ans;
116+
}
117+
};
118+
```
70119
120+
### **Go**
121+
122+
```go
123+
func maxSubsequence(nums []int, k int) []int {
124+
idx := make([]int, len(nums))
125+
for i := range idx {
126+
idx[i] = i
127+
}
128+
sort.Slice(idx, func(i, j int) bool { return nums[idx[i]] > nums[idx[j]] })
129+
sort.Ints(idx[:k])
130+
ans := make([]int, k)
131+
for i, j := range idx[:k] {
132+
ans[i] = nums[j]
133+
}
134+
return ans
135+
}
71136
```
72137

73138
### **TypeScript**

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

+62-1
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,74 @@ Another possible subsequence is [4, 3].
5454
### **Python3**
5555

5656
```python
57-
57+
class Solution:
58+
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
59+
idx = list(range(len(nums)))
60+
idx.sort(key=lambda i: nums[i])
61+
return [nums[i] for i in sorted(idx[-k:])]
5862
```
5963

6064
### **Java**
6165

6266
```java
67+
class Solution {
68+
public int[] maxSubsequence(int[] nums, int k) {
69+
int[] ans = new int[k];
70+
List<Integer> idx = new ArrayList<>();
71+
int n = nums.length;
72+
for (int i = 0; i < n; ++i) {
73+
idx.add(i);
74+
}
75+
idx.sort(Comparator.comparingInt(i -> -nums[i]));
76+
int[] t = new int[k];
77+
for (int i = 0; i < k; ++i) {
78+
t[i] = idx.get(i);
79+
}
80+
Arrays.sort(t);
81+
for (int i = 0; i < k; ++i) {
82+
ans[i] = nums[t[i]];
83+
}
84+
return ans;
85+
}
86+
}
87+
```
88+
89+
### **C++**
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
vector<int> maxSubsequence(vector<int>& nums, int k) {
95+
int n = nums.size();
96+
vector<pair<int, int>> vals;
97+
for (int i = 0; i < n; ++i) vals.push_back({i, nums[i]});
98+
sort(vals.begin(), vals.end(), [&](auto x1, auto x2) {
99+
return x1.second > x2.second;
100+
});
101+
sort(vals.begin(), vals.begin() + k);
102+
vector<int> ans;
103+
for (int i = 0; i < k; ++i) ans.push_back(vals[i].second);
104+
return ans;
105+
}
106+
};
107+
```
63108
109+
### **Go**
110+
111+
```go
112+
func maxSubsequence(nums []int, k int) []int {
113+
idx := make([]int, len(nums))
114+
for i := range idx {
115+
idx[i] = i
116+
}
117+
sort.Slice(idx, func(i, j int) bool { return nums[idx[i]] > nums[idx[j]] })
118+
sort.Ints(idx[:k])
119+
ans := make([]int, k)
120+
for i, j := range idx[:k] {
121+
ans[i] = nums[j]
122+
}
123+
return ans
124+
}
64125
```
65126

66127
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
vector<int> maxSubsequence(vector<int>& nums, int k) {
4+
int n = nums.size();
5+
vector<pair<int, int>> vals;
6+
for (int i = 0; i < n; ++i) vals.push_back({i, nums[i]});
7+
sort(vals.begin(), vals.end(), [&](auto x1, auto x2) {
8+
return x1.second > x2.second;
9+
});
10+
sort(vals.begin(), vals.begin() + k);
11+
vector<int> ans;
12+
for (int i = 0; i < k; ++i) ans.push_back(vals[i].second);
13+
return ans;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func maxSubsequence(nums []int, k int) []int {
2+
idx := make([]int, len(nums))
3+
for i := range idx {
4+
idx[i] = i
5+
}
6+
sort.Slice(idx, func(i, j int) bool { return nums[idx[i]] > nums[idx[j]] })
7+
sort.Ints(idx[:k])
8+
ans := make([]int, k)
9+
for i, j := range idx[:k] {
10+
ans[i] = nums[j]
11+
}
12+
return ans
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int[] maxSubsequence(int[] nums, int k) {
3+
int[] ans = new int[k];
4+
List<Integer> idx = new ArrayList<>();
5+
int n = nums.length;
6+
for (int i = 0; i < n; ++i) {
7+
idx.add(i);
8+
}
9+
idx.sort(Comparator.comparingInt(i -> -nums[i]));
10+
int[] t = new int[k];
11+
for (int i = 0; i < k; ++i) {
12+
t[i] = idx.get(i);
13+
}
14+
Arrays.sort(t);
15+
for (int i = 0; i < k; ++i) {
16+
ans[i] = nums[t[i]];
17+
}
18+
return ans;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
3+
idx = list(range(len(nums)))
4+
idx.sort(key=lambda i: nums[i])
5+
return [nums[i] for i in sorted(idx[-k:])]

0 commit comments

Comments
 (0)