LeetCode solutions

2204. Find Subsequence of Length K With the Largest Sum

My accepted Python solution to LeetCode problem 2204, Find Subsequence of Length K With the Largest Sum, running in 4ms.

  • Difficulty: Easy
  • Python
  • Runtime 4ms
  • Memory 17.7MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 4ms, memory 17.7MB, accepted 2025-12-31.

python
class Solution:
    def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
        # Get indices of k largest elements
        indexed = [(num, i) for i, num in enumerate(nums)]
        indexed.sort(reverse=True)
        indices = sorted([i for num, i in indexed[:k]])
        return [nums[i] for i in indices]

Source