Search
 
SCRIPT & CODE EXAMPLE
 

CPP

longest increasing subsequence nlogn c++

#include <bits/stdc++.h>
using namespace std;
 
int LongestIncreasingSubsequenceLength(std::vector<int>& v)
{
    if (v.size() == 0) // boundary case
        return 0;
 
    std::vector<int> tail(v.size(), 0);
    int length = 1; // always points empty slot in tail
 
    tail[0] = v[0];
 
    for (int i = 1; i < v.size(); i++) {
 
        // Do binary search for the element in
        // the range from begin to begin + length
        auto b = tail.begin(), e = tail.begin() + length;
        auto it = lower_bound(b, e, v[i]);
 
        // If not present change the tail element to v[i]
        if (it == tail.begin() + length)
            tail[length++] = v[i];
        else
            *it = v[i];
    }
 
    return length;
}
 
int main()
{
    std::vector<int> v{ 2, 5, 3, 7, 11, 8, 10, 13, 6 };
    std::cout
        << "Length of Longest Increasing Subsequence is "
        << LongestIncreasingSubsequenceLength(v);
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: inpout in Array c++ 
Cpp :: array list cpp 
Cpp :: split string by delimiter cpp 
Cpp :: void pointer c++ 
Cpp :: sum function in c++ 
Cpp :: Arduino Counting 
Cpp :: c++ loop array 
Cpp :: vector erase iterator 
Cpp :: C++ mutex header 
Cpp :: linux x11 copy paste event 
C :: _CRT_SECURE_NO_WARNINGS 
C :: how to create random integers from a specific range in c language 
C :: how to set a pointer to an offset in c 
C :: Sorting number excluding elements in highest to lowest 
C :: Invalid public key for CUDA apt repository 
C :: bubble sort a linked list in c 
C :: get chunks of a mp4 in ffmpeg 
C :: lsusb command not found 
C :: random in c 
C :: strcasecmp in c 
C :: c int to string 
C :: how to print value of pointer in c 
C :: c program to find minimum of 4 numbers using conditional operator in c 
C :: strong number in c 
C :: c memset 
C :: sleep function in c 
C :: Grepper VSCode Add On 
C :: how to convert int in to const char in c 
C :: pointer to function c 
C :: string array in c 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =