I have a set of integers. I want to find the longest increasing subsequence of that set using dynamic programming.
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