I am trying to construct the longest subsequence from stage 1 of the patience sort. This runs in O(n log n). However, when trying to construct the longest subsequence from the individual piles, I cannot seem to come up with anything faster than O(n^2). The algorithm I am using (in C++) is the following. If further explanation is needed I can do further explanation, I can do so.
vector<int> composeLIS(const vector<vector<int>>& piles)
{
vector<int> LIS;
int prev;
for (auto i = v.end()-1; i >= v.begin(); i--)
{
if (i == v.end() - 1)
{
auto j = i[i->size()-1][0];
LIS.push_back(j);
prev = j;
}
else
{
for (auto j = i->begin(); j < i->end(); j++)
{
if (prev > *j)
{
LIS.insert(LIS.begin(),*j);
prev = *j;
break;
}
}
}
}
return LIS;
}
Is there any way to compose the subsequence in a complexity better than O(n^2)?