Last active
October 24, 2023 12:35
-
-
Save MikuroXina/9a22bbaf430535a96390fb64c4f6f822 to your computer and use it in GitHub Desktop.
Finding lengths of Longest Increasing Subsequence by its length with Rust.
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
pub fn longest_increasing_subsequence<T: Ord>(nums: impl IntoIterator<Item = T>) -> Vec<usize> { | |
let mut nums = nums.into_iter(); | |
let Some(first) = nums.next() else { | |
return vec![]; | |
}; | |
let mut len_by_using = vec![1]; | |
let mut sorted = vec![first]; | |
for num in nums { | |
let lower_bound = sorted.binary_search(&num).unwrap_or_else(|idx| idx); | |
if lower_bound == sorted.len() { | |
sorted.push(num); | |
} else { | |
sorted[lower_bound] = num; | |
} | |
len_by_using.push(sorted.len()); | |
} | |
len_by_using | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment