Skip to content

Commit 220ca43

Browse files
committed
Longest Increasing subsequence using binary search most optimal approach for this problem
1 parent 82dcddc commit 220ca43

File tree

1 file changed

+18
-67
lines changed

1 file changed

+18
-67
lines changed

search/Longest_Increasing_Subsequence_using_binary_search.cpp

Lines changed: 18 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,49 @@
1-
2-
/******************************************************************************
3-
* @details
4-
Given an integer array nums, return the length of the longest strictly
5-
increasing subsequence.
6-
7-
The longest increasing subsequence is described as a subsequence of an array
8-
where: All elements of the subsequence are in increasing order. This subsequence
9-
itself is of the longest length possible.
10-
11-
For solving this problem we have Three Approaches :-
12-
13-
Approach 1 :- Using Brute Force
14-
The first approach that came to your mind is the Brute Force approach where we
15-
generate all subsequences and then manually filter the subsequences whose
16-
elements come in increasing order and then return the longest such subsequence.
17-
Time Complexity :- O(2^n)
18-
It's time complexity is exponential. Therefore we will try some other
19-
approaches.
20-
21-
Approach 2 :- Using Dynamic Programming
22-
To generate all subsequences we will use recursion and in the recursive logic we
23-
will figure out a way to solve this problem. Recursive Logic to solve this
24-
problem:-
25-
1. We only consider the element in the subsequence if the element is grater then
26-
the last element present in the subsequence
27-
2. When we consider the element we will increase the length of subsequence by 1
28-
Time Complexity: O(N*N)
29-
Space Complexity: O(N*N) + O(N)
30-
31-
This approach is better then the previous Brute Force approach so, we can
32-
consider this approach.
33-
34-
But when the Constraints for the problem is very larger then this approach fails
35-
36-
Approach 3 :- Using Binary Search
37-
Other approaches use additional space to create a new subsequence Array.
38-
Instead, this solution uses the existing nums Array to build the subsequence
39-
array. We can do this because the length of the subsequence array will never be
40-
longer than the current index.
41-
42-
Time complexity: O(n∗log(n))
43-
Space complexity: O(1)
44-
45-
This approach consider Most optimal Approach for solving this problem
46-
47-
*******************************************************************************/
48-
49-
#include <cassert> /// for std::assert
50-
#include <iostream> /// for IO operations
51-
#include <vector> /// for std::vector
1+
#include <cassert> /// for std::assert
2+
#include <iostream> /// for IO operations
3+
#include <vector> /// for std::vector
4+
#include <algorithm> /// for std::lower_bound
525

536
/**
54-
* @brief Function to find the length of Longest Increasing Subsequence (LIS)
7+
* @brief Function to find the length of the Longest Increasing Subsequence (LIS)
558
* using Binary Search
56-
* @param nums Input integer array
57-
* @return Length of the longest increasing subsequence
9+
* @param nums The input vector of integers
10+
* @return The length of the longest increasing subsequence
5811
*/
59-
int Longest_Increasing_Subsequence_using_binary_search(std::vector<int>& nums){
60-
61-
if(nums.size() == 0) return 0;
12+
int longest_increasing_subsequence_using_binary_search(std::vector<int>& nums) {
13+
if (nums.empty()) return 0;
6214

6315
std::vector<int> ans;
6416
ans.push_back(nums[0]);
65-
for(int i=1;i<nums.size();i++){
66-
if(nums[i] > ans.back()){
17+
for (int i = 1; i < nums.size(); i++) {
18+
if (nums[i] > ans.back()) {
6719
ans.push_back(nums[i]);
68-
}
69-
else{
70-
int idx = lower_bound(ans.begin(),ans.end(),nums[i]) -ans.begin();
20+
} else {
21+
int idx = std::lower_bound(ans.begin(), ans.end(), nums[i]) - ans.begin();
7122
ans[idx] = nums[i];
7223
}
7324
}
7425
return ans.size();
7526
}
7627

7728
/**
78-
* @brief test implementations
29+
* @brief Test cases for Longest Increasing Subsequence function
7930
* @returns void
8031
*/
8132
static void tests() {
8233
std::vector<int> arr = {10, 9, 2, 5, 3, 7, 101, 18};
83-
assert(Longest_Increasing_Subsequence_using_binary_search(arr) == 4);
34+
assert(longest_increasing_subsequence_using_binary_search(arr) == 4);
8435

8536
std::vector<int> arr2 = {0, 1, 0, 3, 2, 3};
86-
assert(Longest_Increasing_Subsequence_using_binary_search(arr2) == 4);
37+
assert(longest_increasing_subsequence_using_binary_search(arr2) == 4);
8738

8839
std::vector<int> arr3 = {7, 7, 7, 7, 7, 7, 7};
89-
assert(Longest_Increasing_Subsequence_using_binary_search(arr3) == 1);
40+
assert(longest_increasing_subsequence_using_binary_search(arr3) == 1);
9041

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

9445
/**
95-
* @brief Main function
46+
* @brief Main function to run tests
9647
* @returns 0 on exit
9748
*/
9849
int main() {

0 commit comments

Comments
 (0)