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

#1

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]

else:
dp[i][j] = max(dp[i][j-1], dp[i-1][j])

return(dp[m][n])

def lis(self, A):
B = sorted(list(set(A))) #since we want a strictly increasing sequence
#We get rid of the duplicates
return(self.LCS(A,B))``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.6369554,"math_prob":0.9976716,"size":720,"snap":"2021-04-2021-17","text_gpt3_token_len":246,"char_repetition_ratio":0.10195531,"word_repetition_ratio":0.0,"special_character_ratio":0.38194445,"punctuation_ratio":0.1632653,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99965966,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-01-18T07:25:52Z\",\"WARC-Record-ID\":\"<urn:uuid:b10ebad1-f422-4105-ad0a-0c104761435f>\",\"Content-Length\":\"9632\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ddc09cfb-f9bd-49c6-8a15-8be19e241823>\",\"WARC-Concurrent-To\":\"<urn:uuid:706c7882-b004-4fe4-8de6-3eec2becbcbc>\",\"WARC-IP-Address\":\"44.236.167.115\",\"WARC-Target-URI\":\"https://discuss.interviewbit.com/t/python-o-n-2-solution-using-longest-common-subsequence-solution/50966\",\"WARC-Payload-Digest\":\"sha1:SWWC3QGGR5IYJEI4POH2AYEOHMEDTZDE\",\"WARC-Block-Digest\":\"sha1:EK4TTW5DLESS4Y4CFWT3673AD4UTB5PK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-04/CC-MAIN-2021-04_segments_1610703514423.60_warc_CC-MAIN-20210118061434-20210118091434-00178.warc.gz\"}"}