Skip to content

Commit a354908

Browse files
committed
Longest Increasing subsequence using binary search most optimal approach for this problem
1 parent 0ecb6bd commit a354908

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"Local: Longest_Increasing_Subsequence","url":"c:\\Users\\ASUS\\OneDrive\\Desktop\\hack_fest_2024\\C-Plus-Plus\\search\\Longest_Increasing_Subsequence.cpp","tests":[{"id":1728116499174,"input":"","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\Users\\ASUS\\OneDrive\\Desktop\\hack_fest_2024\\C-Plus-Plus\\search\\Longest_Increasing_Subsequence.cpp","group":"local","local":true}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
// Given an integer array nums, return the length of the longest strictly increasing subsequence.
3+
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+
8+
// For solving this problem we have Three Approaches :-
9+
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.
14+
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)
22+
23+
// This approach is better then the previous Brute Force approach so, we can consider this approach.
24+
25+
// But when the Constraints for the problem is very larger then this approach fails
26+
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.
29+
30+
// Time complexity: O(n∗log(n))
31+
// Space complexity: O(1)
32+
33+
// This approach consider Most optimal Approach for solving this problem
34+
35+
// Implementaion:-
36+
37+
#include <iostream>
38+
#include <vector>
39+
using namespace std;
40+
41+
int Longest_Increasing_Subsequence_using_binary_search(vector<int>& nums){
42+
if(nums.size() == 0) return 0;
43+
44+
vector<int> ans;
45+
ans.push_back(nums[0]);
46+
for(int i=1;i<nums.size();i++){
47+
if(nums[i] > ans.back()){
48+
ans.push_back(nums[i]);
49+
}
50+
else{
51+
int idx = lower_bound(ans.begin(),ans.end(),nums[i]) -ans.begin();
52+
ans[idx] = nums[i];
53+
}
54+
}
55+
return ans.size();
56+
}
57+
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;
62+
}

0 commit comments

Comments
 (0)