How to determine the longest increasing subsequence using dynamic programming?

前端 未结 19 2369
醉梦人生
醉梦人生 2020-11-22 10:55

I have a set of integers. I want to find the longest increasing subsequence of that set using dynamic programming.

19条回答
  •  难免孤独
    2020-11-22 11:38

    This can be solved in O(n^2) using Dynamic Programming. Python code for the same would be like:-

    def LIS(numlist):
        LS = [1]
        for i in range(1, len(numlist)):
            LS.append(1)
            for j in range(0, i):
                if numlist[i] > numlist[j] and LS[i]<=LS[j]:
                    LS[i] = 1 + LS[j]
        print LS
        return max(LS)
    
    numlist = map(int, raw_input().split(' '))
    print LIS(numlist)
    

    For input:5 19 5 81 50 28 29 1 83 23

    output would be:[1, 2, 1, 3, 3, 3, 4, 1, 5, 3] 5

    The list_index of output list is the list_index of input list. The value at a given list_index in output list denotes the Longest increasing subsequence length for that list_index.

提交回复
热议问题