TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
find the length of the Longest Increasing Subsequence (LIS) using Binary Search More...
#include <cassert>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdint>
Go to the source code of this file.
Functions | |
template<typename T > | |
std::uint32_t | longest_increasing_subsequence_using_binary_search (std::vector< T > &nums) |
for std::uint32_t | |
static void | tests () |
Test cases for Longest Increasing Subsequence function. | |
int | main () |
Main function to run tests. | |
find the length of the Longest Increasing Subsequence (LIS) using Binary Search
Given an integer array nums, return the length of the longest strictly increasing subsequence. The longest increasing subsequence is described as a subsequence of an array where: All elements of the subsequence are in increasing order. This subsequence itself is of the longest length possible.
For solving this problem we have Three Approaches :-
Approach 1 :- Using Brute Force The first approach that came to your mind is the Brute Force approach where we generate all subsequences and then manually filter the subsequences whose elements come in increasing order and then return the longest such subsequence. Time Complexity :- O(2^n) It's time complexity is exponential. Therefore we will try some other approaches.
Approach 2 :- Using Dynamic Programming To generate all subsequences we will use recursion and in the recursive logic we will figure out a way to solve this problem. Recursive Logic to solve this problem:-
This approach is better then the previous Brute Force approach so, we can consider this approach.
But when the Constraints for the problem is very larger then this approach fails
Approach 3 :- Using Binary Search Other approaches use additional space to create a new subsequence Array. Instead, this solution uses the existing nums Array to build the subsequence array. We can do this because the length of the subsequence array will never be longer than the current index.
Time complexity: O(n∗log(n)) Space complexity: O(1)
This approach consider Most optimal Approach for solving this problem
Definition in file longest_increasing_subsequence_using_binary_search.cpp.
std::uint32_t longest_increasing_subsequence_using_binary_search | ( | std::vector< T > & | nums | ) |
for std::uint32_t
for std::assert for IO operations for std::vector for std::lower_bound
Function to find the length of the Longest Increasing Subsequence (LIS) using Binary Search
T | The type of the elements in the input vector |
nums | The input vector of elements of type T |
Definition at line 65 of file longest_increasing_subsequence_using_binary_search.cpp.
int main | ( | void | ) |
Main function to run tests.
Definition at line 114 of file longest_increasing_subsequence_using_binary_search.cpp.
|
static |
Test cases for Longest Increasing Subsequence function.
Definition at line 85 of file longest_increasing_subsequence_using_binary_search.cpp.