java.lang.Object
g2001_2100.s2099_find_subsequence_of_length_k_with_the_largest_sum.Solution

public class Solution extends Object
2099 - Find Subsequence of Length K With the Largest Sum.<p>Easy</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. You want to find a <strong>subsequence</strong> of <code>nums</code> of length <code>k</code> that has the <strong>largest</strong> sum.</p> <p>Return <em><strong>any</strong> such subsequence as an integer array of length</em> <code>k</code>.</p> <p>A <strong>subsequence</strong> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [2,1,3,3], k = 2</p> <p><strong>Output:</strong> [3,3]</p> <p><strong>Explanation:</strong> The subsequence has the largest sum of 3 + 3 = 6.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [-1,-2,3,4], k = 3</p> <p><strong>Output:</strong> [-1,3,4]</p> <p><strong>Explanation:</strong> The subsequence has the largest sum of -1 + 3 + 4 = 6.</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = [3,4,3,3], k = 2</p> <p><strong>Output:</strong> [3,4]</p> <p><strong>Explanation:</strong> The subsequence has the largest sum of 3 + 4 = 7.</p> <p>Another possible subsequence is [4, 3].</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 1000</code></li> <li><code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code></li> <li><code>1 <= k <= nums.length</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maxSubsequence

      public int[] maxSubsequence(int[] nums, int k)