Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Find the Most Competitive Subsequence
Bookmark
Brute Force
Monotonic Stack
Input
Standard
Example 2
Simple
Custom
nums
=
[3, 5, 2, 6]
,
k
=
2
nums
3
5
2
6
0
1
2
3
try every length-2 subsequence; keep the lexicographically smallest
nums
3
5
2
6
0
1
2
3
try every length-2 subsequence; keep the lexicographically smallest
candidate #1
more competitive ✓
nums
3
5
2
6
0
1
2
3
candidate
3
5
best so far
3
5
new most competitive subsequence!
candidate #2
nums
3
5
2
6
0
1
2
3
candidate
3
2
best so far
3
2
compare this candidate to the best so far…
candidate #3
nums
3
5
2
6
0
1
2
3
candidate
3
6
best so far
3
2
compare this candidate to the best so far…
candidate #3
not better ✗
nums
3
5
2
6
0
1
2
3
candidate
3
6
best so far
3
2
keep the previous best
candidate #4
not better ✗
nums
3
5
2
6
0
1
2
3
candidate
5
2
best so far
3
2
keep the previous best
candidate #5
nums
3
5
2
6
0
1
2
3
candidate
5
6
best so far
3
2
compare this candidate to the best so far…
candidate #6
nums
3
5
2
6
0
1
2
3
candidate
2
6
best so far
2
6
compare this candidate to the best so far…
candidate #6
more competitive ✓
nums
3
5
2
6
0
1
2
3
candidate
2
6
best so far
2
6
new most competitive subsequence!
nums
3
5
2
6
0
1
2
3
most competitive
2
6
answer =
[2, 6]
algo
master
.
io
Step:
Brute force: try every length-2 subsequence, keep the most competitive.
0 / 14
Input
Standard
Example 2
Simple
Custom
nums
=
[3, 5, 2, 6]
,
k
=
2
0 / 14
algo
master
.
io
Step:
Brute force: try every length-2 subsequence, keep the most competitive.