Python O(n^2) solution using Longest Common Subsequence solution


Answer to this problem is simply the longest common sub-sequence between A and sorted(set(A)), where set(A) contains only one copy of every distinct element in A. Eg.: A = [4,1,1,2,3,3], set(A) = [4,1,2,3].
NOTE: We can further reduce the space complexity to O(n).

class Solution:
    # @param A : tuple of integers
    # @return an integer
    def LCS(self,A,B):
        n = len(A)
        m = len(B)
        dp = [[0 for j in range(n+1)] for i in range(m+1)]
        for i in range(1,m+1):
            for j in range(1,n+1):
                if A[j-1]==B[i-1]:
                    dp[i][j] = 1 + dp[i-1][j-1]
                    dp[i][j] = max(dp[i][j-1], dp[i-1][j])

    def lis(self, A):
        B = sorted(list(set(A))) #since we want a strictly increasing sequence
        #We get rid of the duplicates