2099. Find Subsequence of Length K With the Largest Sum Easy

@problem@discussion
#Array#Hash Table#Sorting#Heap (Priority Queue)



1/**
2 * [2099] Find Subsequence of Length K With the Largest Sum
3 *
4 * You are given an integer array nums and an integer k. You want to find a subsequence of nums of length k that has the largest sum.
5 * Return any such subsequence as an integer array of length k.
6 * A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
7 *  
8 * Example 1:
9 * 
10 * Input: nums = [2,1,3,3], k = 2
11 * Output: [3,3]
12 * Explanation:
13 * The subsequence has the largest sum of 3 + 3 = 6.
14 * Example 2:
15 * 
16 * Input: nums = [-1,-2,3,4], k = 3
17 * Output: [-1,3,4]
18 * Explanation: 
19 * The subsequence has the largest sum of -1 + 3 + 4 = 6.
20 * 
21 * Example 3:
22 * 
23 * Input: nums = [3,4,3,3], k = 2
24 * Output: [3,4]
25 * Explanation:
26 * The subsequence has the largest sum of 3 + 4 = 7. 
27 * Another possible subsequence is [4, 3].
28 * 
29 *  
30 * Constraints:
31 * 
32 * 	1 <= nums.length <= 1000
33 * 	-10^5 <= nums[i] <= 10^5
34 * 	1 <= k <= nums.length
35 * 
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/
40// discuss: https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45    pub fn max_subsequence(nums: Vec<i32>, k: i32) -> Vec<i32> {
46        vec![]
47    }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_2099() {
58    }
59}
60


Back
© 2025 bowen.ge All Rights Reserved.