Skip to content

Commit 8697330

Browse files
committed
Longest Increasing subsequence using binary search most optimal approach for this problem (Modified)
1 parent 148bcc3 commit 8697330

File tree

1 file changed

+72
-33
lines changed

1 file changed

+72
-33
lines changed
Lines changed: 72 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,66 @@
11

2-
// Given an integer array nums, return the length of the longest strictly increasing subsequence.
2+
/******************************************************************************
3+
* @details
4+
Given an integer array nums, return the length of the longest strictly
5+
increasing subsequence.
36
4-
// The longest increasing subsequence is described as a subsequence of an array where:
5-
// All elements of the subsequence are in increasing order.
6-
// This subsequence itself is of the longest length possible.
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.
710
8-
// For solving this problem we have Three Approaches :-
11+
For solving this problem we have Three Approaches :-
912
10-
// Approach 1 :- Using Brute Force
11-
// 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.
12-
// Time Complexity :- O(2^n)
13-
// It's time complexity is exponential. Therefore we will try some other approaches.
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.
1420
15-
// Approach 2 :- Using Dynamic Programming
16-
// To generate all subsequences we will use recursion and in the recursive logic we will figure out a way to solve this problem.
17-
// Recursive Logic to solve this problem:-
18-
// 1. We only consider the element in the subsequence if the element is grater then the last element present in the subsequence
19-
// 2. When we consider the element we will increase the length of subsequence by 1
20-
// Time Complexity: O(N*N)
21-
// Space Complexity: O(N*N) + O(N)
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)
2230
23-
// This approach is better then the previous Brute Force approach so, we can consider this approach.
31+
This approach is better then the previous Brute Force approach so, we can
32+
consider this approach.
2433
25-
// But when the Constraints for the problem is very larger then this approach fails
34+
But when the Constraints for the problem is very larger then this approach fails
2635
27-
// Approach 3 :- Using Binary Search
28-
// 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.
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.
2941
30-
// Time complexity: O(n∗log(n))
31-
// Space complexity: O(1)
42+
Time complexity: O(n∗log(n))
43+
Space complexity: O(1)
3244
33-
// This approach consider Most optimal Approach for solving this problem
45+
This approach consider Most optimal Approach for solving this problem
3446
35-
// Implementaion:-
47+
*******************************************************************************/
3648

37-
#include <iostream>
38-
#include <vector>
39-
using namespace std;
49+
#include <cassert> /// for std::assert
50+
#include <iostream> /// for IO operations
51+
#include <vector> /// for std::vector
4052

41-
int Longest_Increasing_Subsequence_using_binary_search(vector<int>& nums){
53+
/**
54+
* @brief Function to find the length of Longest Increasing Subsequence (LIS)
55+
* using Binary Search
56+
* @param nums Input integer array
57+
* @return Length of the longest increasing subsequence
58+
*/
59+
int Longest_Increasing_Subsequence_using_binary_search(std::vector<int>& nums){
60+
4261
if(nums.size() == 0) return 0;
4362

44-
vector<int> ans;
63+
std::vector<int> ans;
4564
ans.push_back(nums[0]);
4665
for(int i=1;i<nums.size();i++){
4766
if(nums[i] > ans.back()){
@@ -55,8 +74,28 @@ int Longest_Increasing_Subsequence_using_binary_search(vector<int>& nums){
5574
return ans.size();
5675
}
5776

58-
int main(){
59-
vector<int> arr = {10,9,2,5,3,7,101,18};
60-
cout<<"longest increasing subsequence : "<<Longest_Increasing_Subsequence_using_binary_search(arr);
61-
return 0;
77+
/**
78+
* @brief test implementations
79+
* @returns void
80+
*/
81+
static void tests() {
82+
std::vector<int> arr = {10, 9, 2, 5, 3, 7, 101, 18};
83+
assert(Longest_Increasing_Subsequence_using_binary_search(arr) == 4);
84+
85+
std::vector<int> arr2 = {0, 1, 0, 3, 2, 3};
86+
assert(Longest_Increasing_Subsequence_using_binary_search(arr2) == 4);
87+
88+
std::vector<int> arr3 = {7, 7, 7, 7, 7, 7, 7};
89+
assert(Longest_Increasing_Subsequence_using_binary_search(arr3) == 1);
90+
91+
std::cout << "All tests have successfully passed!\n";
92+
}
93+
94+
/**
95+
* @brief Main function
96+
* @returns 0 on exit
97+
*/
98+
int main() {
99+
tests();
100+
return 0;
62101
}

0 commit comments

Comments
 (0)