-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lecture 119: Longest Increasing Subsequence
- Loading branch information
1 parent
56642f3
commit 48592c9
Showing
8 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
class Solution { | ||
public: | ||
int maxEnvelopes(vector<vector<int>>& envelopes) { | ||
// Step 1: Sort the envelopes by width in ascending order. | ||
// If two envelopes have the same width, sort by height in descending order. | ||
sort(envelopes.begin(), envelopes.end(), [](vector<int>& a, vector<int>& b) { | ||
|
||
// If widths are the same, sort by height in descending order | ||
if (a[0] == b[0]) { | ||
return a[1] > b[1]; // Return true if 'a' should come before 'b' based on height | ||
} | ||
|
||
// Otherwise, sort by width in ascending order | ||
return a[0] < b[0]; // Return true if 'a' should come before 'b' based on width | ||
}); | ||
|
||
int n = envelopes.size(); | ||
|
||
// Step 2: Initialize a vector `ans` to keep track of the longest increasing subsequence of heights. | ||
// This will represent the sequence of envelopes we can Russian doll. | ||
vector<int> ans; | ||
|
||
// Add the height of the first envelope to start our sequence. | ||
ans.push_back(envelopes[0][1]); | ||
|
||
// Step 3: Traverse through each envelope to build the longest increasing subsequence. | ||
for (int i = 1; i < n; i++) { | ||
// Check if the current envelope's height is greater than the last height in `ans`. | ||
// If yes, we can add it to our sequence (Russian-dolling one more envelope). | ||
if (envelopes[i][1] > ans.back()) { | ||
ans.push_back(envelopes[i][1]); | ||
} | ||
else { | ||
// Otherwise, find the position where this height would fit in `ans` to keep it sorted. | ||
// Use binary search to find the first height in `ans` that is >= envelopes[i][1] | ||
int index = lower_bound(ans.begin(), ans.end(), envelopes[i][1]) - ans.begin(); | ||
|
||
// Replace the value at `index` with the current height to maintain an increasing sequence | ||
// with smaller values where possible (helps in maximizing the sequence). | ||
ans[index] = envelopes[i][1]; | ||
} | ||
} | ||
|
||
// Step 4: The size of `ans` will be the maximum number of envelopes we can Russian doll. | ||
return ans.size(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Lecture Codes | ||
|
||
| **Problem** | **Solution** | **Level** | | ||
|:--------------|:--------------:|:-----------:| | ||
| [354. Russian Doll Envelopes](https://leetcode.com/problems/russian-doll-envelopes/description/) | [Solution]() | Hard | | ||
| [Longest Increasing Subsequence](https://www.geeksforgeeks.org/problems/longest-increasing-subsequence-1587115620/1) | [Solution1](), [Solution2](), [Solution3](), [Solution4](), [Solution5]() | Medium | |
28 changes: 28 additions & 0 deletions
28
Lectures/Lecture_119/Lecture_Codes/longest_increasing_subsequence_1.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Recursion [TLE] | ||
|
||
class Solution { | ||
public: | ||
int solve(int n, int a[], int currIndex, int prevIndex) { | ||
if (currIndex == n) { | ||
return 0; | ||
} | ||
|
||
// include curr number | ||
int include = 0; | ||
if (prevIndex == -1 || a[currIndex] > a[prevIndex]) { | ||
include = 1 + solve(n , a, currIndex+1, currIndex); | ||
} | ||
|
||
// exclude curr number | ||
int exclude = 0; | ||
exclude = 0 + solve(n, a, currIndex+1, prevIndex); | ||
|
||
return max(include, exclude); | ||
} | ||
|
||
//Function to find length of longest increasing subsequence. | ||
int longestSubsequence(int n, int a[]) { | ||
// your code here | ||
return solve(n, a, 0, -1); | ||
} | ||
}; |
36 changes: 36 additions & 0 deletions
36
Lectures/Lecture_119/Lecture_Codes/longest_increasing_subsequence_2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Recursion + Memoization [TLE] | ||
|
||
class Solution { | ||
public: | ||
int solve(int n, int a[], int currIndex, int prevIndex, vector<vector<int>> dp) { | ||
if (currIndex == n) { | ||
return 0; | ||
} | ||
|
||
if (dp[currIndex][prevIndex+1] != -1) { | ||
return dp[currIndex][prevIndex+1]; | ||
} | ||
|
||
// include curr number | ||
int include = 0; | ||
if (prevIndex == -1 || a[currIndex] > a[prevIndex]) { | ||
include = 1 + solve(n , a, currIndex+1, currIndex, dp); | ||
} | ||
|
||
// exclude curr number | ||
int exclude = 0; | ||
exclude = 0 + solve(n, a, currIndex+1, prevIndex, dp); | ||
|
||
dp[currIndex][prevIndex+1] = max(include, exclude); | ||
|
||
return dp[currIndex][prevIndex+1]; | ||
} | ||
|
||
//Function to find length of longest increasing subsequence. | ||
int longestSubsequence(int n, int a[]) { | ||
// your code here | ||
vector<vector<int>> dp(n, vector<int> (n+1, -1)); | ||
|
||
return solve(n, a, 0, -1, dp); | ||
} | ||
}; |
34 changes: 34 additions & 0 deletions
34
Lectures/Lecture_119/Lecture_Codes/longest_increasing_subsequence_3.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Tabulation Method [TLE] | ||
|
||
class Solution { | ||
public: | ||
int solve(int n, int a[]) { | ||
|
||
vector<vector<int>> dp(n+1, vector<int> (n+1, 0)); | ||
|
||
for (int currIndex = n-1; currIndex >= 0; currIndex--) { | ||
for (int prevIndex = currIndex-1; prevIndex >= -1; prevIndex--) { | ||
|
||
// include curr number | ||
int include = 0; | ||
if (prevIndex == -1 || a[currIndex] > a[prevIndex]) { | ||
include = 1 + dp[currIndex+1][currIndex+1]; | ||
} | ||
|
||
// exclude curr number | ||
int exclude = 0 + dp[currIndex+1][prevIndex+1]; | ||
|
||
dp[currIndex][prevIndex+1] = max(include, exclude); | ||
} | ||
} | ||
|
||
return dp[0][0]; | ||
} | ||
|
||
//Function to find length of longest increasing subsequence. | ||
int longestSubsequence(int n, int a[]) { | ||
// your code here | ||
|
||
return solve(n, a); | ||
} | ||
}; |
36 changes: 36 additions & 0 deletions
36
Lectures/Lecture_119/Lecture_Codes/longest_increasing_subsequence_4.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Space Optimization [TLE] | ||
|
||
class Solution { | ||
public: | ||
int solve(int n, int a[]) { | ||
|
||
vector<int> currRow(n+1, 0); | ||
vector<int> nextRow(n+1, 0); | ||
|
||
for (int currIndex = n-1; currIndex >= 0; currIndex--) { | ||
for (int prevIndex = currIndex-1; prevIndex >= -1; prevIndex--) { | ||
|
||
// include curr number | ||
int include = 0; | ||
if (prevIndex == -1 || a[currIndex] > a[prevIndex]) { | ||
include = 1 + nextRow[currIndex+1]; | ||
} | ||
|
||
// exclude curr number | ||
int exclude = 0 + nextRow[prevIndex+1]; | ||
|
||
currRow[prevIndex+1] = max(include, exclude); | ||
} | ||
nextRow = currRow; | ||
} | ||
|
||
return nextRow[0]; | ||
} | ||
|
||
//Function to find length of longest increasing subsequence. | ||
int longestSubsequence(int n, int a[]) { | ||
// your code here | ||
|
||
return solve(n, a); | ||
} | ||
}; |
33 changes: 33 additions & 0 deletions
33
Lectures/Lecture_119/Lecture_Codes/longest_increasing_subsequence_5.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Optimal solution [Binary Search With DP] | ||
|
||
class Solution { | ||
public: | ||
int solve(int n, int a[]) { | ||
|
||
if (n == 0) { | ||
return 0; | ||
} | ||
|
||
vector<int> ans; | ||
ans.push_back(a[0]); | ||
|
||
for (int i=1; i<n; i++) { | ||
|
||
if (a[i] > ans.back()) { | ||
ans.push_back(a[i]); | ||
} | ||
|
||
int index = lower_bound(ans.begin(), ans.end(), a[i]) - ans.begin(); | ||
ans[index] = a[i]; | ||
} | ||
|
||
return ans.size(); | ||
} | ||
|
||
//Function to find length of longest increasing subsequence. | ||
int longestSubsequence(int n, int a[]) { | ||
// your code here | ||
|
||
return solve(n, a); | ||
} | ||
}; |