Find the Most Competitive Subsequence - Problem

Find the Most Competitive Subsequence

Given an integer array nums and a positive integer k, you need to find the most competitive subsequence of nums with exactly k elements.

A subsequence is formed by removing some (possibly zero) elements from the original array while maintaining the relative order of remaining elements. Think of it as selecting elements from left to right without changing their positions.

What makes one subsequence "more competitive" than another? Simple - it's lexicographically smaller! When comparing two subsequences of the same length, the one that has a smaller number at the first differing position wins.

Example: [1,3,4] beats [1,3,5] because at the third position, 4 < 5.

Your goal is to find the subsequence that would come first if all possible subsequences of size k were sorted in ascending order.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [3,5,2,6], k = 2
โ€บ Output: [2,6]
๐Ÿ’ก Note: Among all possible subsequences of size 2: [3,5], [3,2], [3,6], [5,2], [5,6], [2,6]. The subsequence [2,6] is the most competitive because it starts with 2, which is the smallest possible first element.
example_2.py โ€” Longer Subsequence
$ Input: nums = [2,4,3,3,5,4,9,6], k = 4
โ€บ Output: [2,3,3,4]
๐Ÿ’ก Note: We need to select 4 elements. Starting with 2 (smallest possible), then 3, then another 3, and finally 4. This gives us the lexicographically smallest subsequence of length 4.
example_3.py โ€” Edge Case
$ Input: nums = [71,18,52,29,55,73,24,42,66,8,80,2], k = 3
โ€บ Output: [18,24,2]
๐Ÿ’ก Note: Even though 8 and 2 are the two smallest numbers, we can't use both while maintaining order. The optimal solution is [18,24,2] which is lexicographically smallest among all valid subsequences.

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 0 โ‰ค nums[i] โ‰ค 109
  • 1 โ‰ค k โ‰ค nums.length
  • The answer is guaranteed to be unique

Visualization

Tap to expand
๐Ÿ† Most Competitive Team SelectionAvailable Players (maintain order): [3, 5, 2, 6] โ†’ Select best 23526Step 1: Add player 3 โ†’ Team: [3]Step 2: Add player 5 โ†’ Team: [3,5]Step 3: Player 2 appears! Remove 5 (weaker), remove 3 (weaker) โ†’ Team: [2]Step 4: Add player 6 โ†’ Team: [2,6] โœ“๐Ÿ† Optimal Team: [2, 6]Most competitive subsequence found!
Understanding the Visualization
1
Scan Players Left to Right
Go through each player in formation order
2
Smart Elimination
Remove weaker players from roster when stronger ones appear, if we can still complete the team
3
Build Optimal Team
Maintain the strongest possible team at each step
Key Takeaway
๐ŸŽฏ Key Insight: Use monotonic stack to greedily build the lexicographically smallest subsequence by removing larger elements when smaller ones appear, ensuring we can still complete a valid subsequence.
Asked in
Google 45 Amazon 38 Meta 25 Microsoft 18
42.3K Views
Medium-High Frequency
~18 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen