Shortest Longest Increasing Subsequence Code

Python 3, 66

Note that all numbers are in range [1, 999], we can use an array b to maintain the longest subsequence length ending with each number. b[x] = d means that the longest subsequence ending with x has length d. For each number from the input, we update the array using b[x] = max(b[:x]) + 1 and then we got the job done by taking max(b) finally.

The time complexity is O(n) O(m n), where m is always 1000 and n is the number of input elements.

def f(a):
 for x in a:b[x]=max(b[:x])+1
 return max(b)

Wow, looks like already ungolfed :) You can test it using stdin/stdout by adding a line:


Pyth, 26 29 33 39

J*]0^[email protected]:J0k)eSJ

Port of @ray's solution. Passes official tests. Now uses space-separated STDIN input, not function call.

Run as follows:

./ -c "J*]0^[email protected]:J0k)eSJ" <<< "1 5 7 2 8 4 3 5"


J*]0^T3                 J = [0]*10^3
Fkyw                    For k in space_sep(input()):
[email protected]                    J[k]=
heS:J0k                 max(J[0:k])+1
)                       end for
eSJ                     max(J)

Time unlimited:

Pyth, 18


Technical note: I noticed a bug in my Pyth complier while writing this golf. L wasn't working. That's why there is a recent commit to the above git repository.

Python - 113

for i in map(int,input().split()):
 if not a or i>a[-1]:a+=[i]
 while a[z]<i:z+=1


Code Golf