How to determine the longest increasing subsequence using dynamic programming?

前端 未结 19 2749
醉梦人生
醉梦人生 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:34

    Simplest LIS solution in C++ with O(nlog(n)) time complexity

    #include 
    #include "vector"
    using namespace std;
    
    // binary search (If value not found then it will return the index where the value should be inserted)
    int ceilBinarySearch(vector &a,int beg,int end,int value)
    {
        if(beg<=end)
        {
            int mid = (beg+end)/2;
            if(a[mid] == value)
                return mid;
            else if(value < a[mid])
                return ceilBinarySearch(a,beg,mid-1,value);
            else
                return ceilBinarySearch(a,mid+1,end,value);
    
        return 0;
        }
    
        return beg;
    
    }
    int lis(vector arr)
    {
        vector dp(arr.size(),0);
        int len = 0;
        for(int i = 0;i arr  {2, 5,-1,0,6,1,2};
        cout<

    OUTPUT:
    4

提交回复
热议问题