Find the Most Competitive Subsequence

Given an integer array nums and a positive integer k, return the most competitive subsequence of nums of size k.

An array's subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.

A subsequence a is more competitive than a subsequence b of the same length if, in the first position where a and b differ, subsequence a has a number less than the corresponding number in b.

Example 1
Inputnums = [3,5,2,6], k = 2
Output[2,6]
Among every possible subsequence of size 2, [2, 6] is the most competitive.
Example 2
Inputnums = [2,4,3,3,5,4,9,6], k = 4
Output[2,3,3,4]
The most competitive subsequence of size 4 is [2, 3, 3, 4].

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^9
  • 1 <= k <= nums.length

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate