Skip to content

Longest Increasing subsequence using binary search most optimal approach (Modified) #2776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Oct 18, 2024
Merged
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a354908
Longest Increasing subsequence using binary search most optimal appro…
namanmodi65 Oct 5, 2024
148bcc3
Longest Increasing subsequence using binary search most optimal appro…
namanmodi65 Oct 5, 2024
002d564
Merge branch 'TheAlgorithms:master' into master
namanmodi65 Oct 5, 2024
8697330
Longest Increasing subsequence using binary search most optimal appro…
namanmodi65 Oct 5, 2024
82dcddc
Merge branch 'master' of github.com:namanmodi65/C-Plus-Plus
namanmodi65 Oct 5, 2024
c084298
Merge branch 'TheAlgorithms:master' into master
namanmodi65 Oct 6, 2024
2c96392
Merge branch 'master' into master
realstealthninja Oct 7, 2024
b3037bf
Merge branch 'TheAlgorithms:master' into master
namanmodi65 Oct 7, 2024
220ca43
Longest Increasing subsequence using binary search most optimal appro…
namanmodi65 Oct 7, 2024
8aca8dd
Merge branch 'master' of github.com:namanmodi65/C-Plus-Plus
namanmodi65 Oct 7, 2024
a9c309e
Longest Increasing subsequence using binary search most optimal appro…
namanmodi65 Oct 7, 2024
97a4b9c
Merge branch 'master' into master
namanmodi65 Oct 9, 2024
5d87f42
Longest Increasing subsequence using binary search most optimal appro…
namanmodi65 Oct 10, 2024
94a8017
Merge branch 'master' of github.com:namanmodi65/C-Plus-Plus
namanmodi65 Oct 10, 2024
9d42145
Merge branch 'TheAlgorithms:master' into master
namanmodi65 Oct 10, 2024
245b998
Floyd warshall
namanmodi65 Oct 10, 2024
f336108
Merge branch 'master' of github.com:namanmodi65/C-Plus-Plus
namanmodi65 Oct 10, 2024
c1b18f1
Longest Increasing subsequence using binary search most optimal appro…
namanmodi65 Oct 10, 2024
2bc2784
Merge branch 'master' into master
namanmodi65 Oct 10, 2024
1f0e17e
Longest Increasing subsequence using binary search most optimal appro…
namanmodi65 Oct 11, 2024
eeda897
Merge branch 'master' of github.com:namanmodi65/C-Plus-Plus
namanmodi65 Oct 11, 2024
01f2ad6
Merge branch 'TheAlgorithms:master' into master
namanmodi65 Oct 11, 2024
0563264
Longest Increasing subsequence using binary search most optimal appro…
namanmodi65 Oct 12, 2024
c3edc82
Merge branch 'master' into master
realstealthninja Oct 13, 2024
2c2d9b2
Merge branch 'master' into master
realstealthninja Oct 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions search/Longest_Increasing_Subsequence_using_binary_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@

/******************************************************************************
* @details
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:-
1. We only consider the element in the subsequence if the element is grater then
the last element present in the subsequence
2. When we consider the element we will increase the length of subsequence by 1
Time Complexity: O(N*N)
Space Complexity: O(N*N) + O(N)

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

*******************************************************************************/

#include <cassert> /// for std::assert
#include <iostream> /// for IO operations
#include <vector> /// for std::vector

/**
* @brief Function to find the length of Longest Increasing Subsequence (LIS)
* using Binary Search
* @param nums Input integer array
* @return Length of the longest increasing subsequence
*/
int Longest_Increasing_Subsequence_using_binary_search(std::vector<int>& nums){

if(nums.size() == 0) return 0;

std::vector<int> ans;
ans.push_back(nums[0]);
for(int i=1;i<nums.size();i++){
if(nums[i] > ans.back()){
ans.push_back(nums[i]);
}
else{
int idx = lower_bound(ans.begin(),ans.end(),nums[i]) -ans.begin();
ans[idx] = nums[i];
}
}
return ans.size();
}

/**
* @brief test implementations
* @returns void
*/
static void tests() {
std::vector<int> arr = {10, 9, 2, 5, 3, 7, 101, 18};
assert(Longest_Increasing_Subsequence_using_binary_search(arr) == 4);

std::vector<int> arr2 = {0, 1, 0, 3, 2, 3};
assert(Longest_Increasing_Subsequence_using_binary_search(arr2) == 4);

std::vector<int> arr3 = {7, 7, 7, 7, 7, 7, 7};
assert(Longest_Increasing_Subsequence_using_binary_search(arr3) == 1);

std::cout << "All tests have successfully passed!\n";
}

/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
tests();
return 0;
}