#2099
Find Subsequence of Length K With the Largest Sum
pupil · 400 · lc easy +24 · verified · 57.4% accepted · 1,755 likes · top 53%
Description
You are given an integer array nums and an integer k. Select a subsequence of nums of length k that achieves the maximum possible sum.
Return any such subsequence as an integer array of length k.
A subsequence is obtained from another array by removing zero or more elements while preserving the order of the remaining elements.
Example 1:
Input: nums = [2,1,3,3], k = 2
Output: [3,3]
Explanation:
The subsequence has the largest sum of 3 + 3 = 6.
Example 2:
Input: nums = [-1,-2,3,4], k = 3
Output: [-1,3,4]
Explanation:
The subsequence has the largest sum of -1 + 3 + 4 = 6.
Example 3:
Input: nums = [3,4,3,3], k = 2
Output: [3,4]
Explanation:
The subsequence has the largest sum of 3 + 4 = 7.
Another possible subsequence is [4, 3].
Code
1
2
3