This is a standard Dynamic programming Question LIS PROBLEM
I want a longest increasing subsequence for points in 2D coordinates
that is, 2 points A(x1,y1) at index i in array , B(x2,y2) at index j in array can be a part of increasing sequence if (x1<=x2) && (y1 <=y2) && !(x1==x2 && y1==y2) && (j>i)
my code is as below which is O(N^2) using standard DP :-
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct Pair
{
int x;
int y;
};
int main()
{
int n;
cin>>n;
vector<Pair> arr;
int L[1000000];
Pair a;
int i;int Maxchain=0;
for(i=0;i<n;i++)
{
cin>>a.x>>a.y;
arr.push_back(a);
L[i]=0;
for (int j = i-1; j >=0; j--)
{
if ((L[j]>(Maxchain-1))&&(L[j]>=L[i])&&(arr[j].x <= arr[i].x) && (arr[j].y <= arr[i].y) && !(arr[j].x == arr[i].x && arr[j].y == arr[i].y))
L[i] = L[j]+1;
}
Maxchain = L[i]>Maxchain ?L[i]:Maxchain ;
}
cout<<Maxchain;
return 0;
}
This is an O(N^2) solution can it be further reduced or any alogrithm for this to solve in O(NlogN) or O(Nlog^2N) ?
for reference found something here:
Longest Increasing Subsequence (LIS) with two numbers
The second answer is more appropriate for my case but how can we implement that?
Need a Better answer or algorithm.