0

I want to find the maximal subset of an array which is sorted in ascending order

Say I have

a = [2, 1, 4, 6, 7]
a_max_subset = [1, 4, 6, 7]

b = [4, 1, 2]
b_max_subset = [1, 2] 

c = [2, 5, 13, 8, 6, 23, 33]
c_max_subset = [2, 5, 8, 23, 33]

Is there an efficient way to do this ?

vin
  • 960
  • 2
  • 14
  • 28

1 Answers1

0

What you are trying to find out is Longest increasing subsequence.

You can find an O(N^2) implementation here. It's well explained and commented properly. Try to understand the explanation and implement it by yourself.

If you are really curious, you can check this O(N logN) solution also.

Karthik
  • 4,950
  • 6
  • 35
  • 65