#1673

Find the Most Competitive Subsequence

specialist · 830 · lc medium +31 · verified · 52.7% accepted · 2,181 likes · top 43%

Description

Given an integer array nums and a positive integer k, find the most competitive subsequence of nums with length k. A subsequence a is more competitive than b if at the first index where they differ, a has a smaller value.

Example 1:

Input: nums = [3,5,2,6], k = 2
Output: [2,6]
Explanation: Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.

Example 2:

Input: nums = [2,4,3,3,5,4,9,6], k = 4
Output: [2,3,3,4]

Code

1
2
3