-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Changes from 10 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 148bcc3
Longest Increasing subsequence using binary search most optimal appro…
namanmodi65 002d564
Merge branch 'TheAlgorithms:master' into master
namanmodi65 8697330
Longest Increasing subsequence using binary search most optimal appro…
namanmodi65 82dcddc
Merge branch 'master' of github.com:namanmodi65/C-Plus-Plus
namanmodi65 c084298
Merge branch 'TheAlgorithms:master' into master
namanmodi65 2c96392
Merge branch 'master' into master
realstealthninja b3037bf
Merge branch 'TheAlgorithms:master' into master
namanmodi65 220ca43
Longest Increasing subsequence using binary search most optimal appro…
namanmodi65 8aca8dd
Merge branch 'master' of github.com:namanmodi65/C-Plus-Plus
namanmodi65 a9c309e
Longest Increasing subsequence using binary search most optimal appro…
namanmodi65 97a4b9c
Merge branch 'master' into master
namanmodi65 5d87f42
Longest Increasing subsequence using binary search most optimal appro…
namanmodi65 94a8017
Merge branch 'master' of github.com:namanmodi65/C-Plus-Plus
namanmodi65 9d42145
Merge branch 'TheAlgorithms:master' into master
namanmodi65 245b998
Floyd warshall
namanmodi65 f336108
Merge branch 'master' of github.com:namanmodi65/C-Plus-Plus
namanmodi65 c1b18f1
Longest Increasing subsequence using binary search most optimal appro…
namanmodi65 2bc2784
Merge branch 'master' into master
namanmodi65 1f0e17e
Longest Increasing subsequence using binary search most optimal appro…
namanmodi65 eeda897
Merge branch 'master' of github.com:namanmodi65/C-Plus-Plus
namanmodi65 01f2ad6
Merge branch 'TheAlgorithms:master' into master
namanmodi65 0563264
Longest Increasing subsequence using binary search most optimal appro…
namanmodi65 c3edc82
Merge branch 'master' into master
realstealthninja 2c2d9b2
Merge branch 'master' into master
realstealthninja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
search/Longest_Increasing_Subsequence_using_binary_search.cpp
This file contains hidden or 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,52 @@ | ||
#include <cassert> /// for std::assert | ||
#include <iostream> /// for IO operations | ||
#include <vector> /// for std::vector | ||
#include <algorithm> /// for std::lower_bound | ||
|
||
/** | ||
* @brief Function to find the length of the Longest Increasing Subsequence (LIS) | ||
* using Binary Search | ||
* @param nums The input vector of integers | ||
* @return The length of the longest increasing subsequence | ||
*/ | ||
int longest_increasing_subsequence_using_binary_search(std::vector<int>& nums) { | ||
realstealthninja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
realstealthninja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (nums.empty()) 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 = std::lower_bound(ans.begin(), ans.end(), nums[i]) - ans.begin(); | ||
ans[idx] = nums[i]; | ||
} | ||
} | ||
return ans.size(); | ||
} | ||
|
||
/** | ||
* @brief Test cases for Longest Increasing Subsequence function | ||
* @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}; | ||
realstealthninja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 to run tests | ||
* @returns 0 on exit | ||
*/ | ||
int main() { | ||
tests(); | ||
realstealthninja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.