Skip to content

Commit 1ca5f59

Browse files
authored
feat(docs): add better docs for longest_increasing_subsequence.rs (#63)
1 parent 53d784f commit 1ca5f59

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/dynamic_programming/longest_increasing_subsequence.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1-
/// Finds the longest increasing subsequence and returns it.
2-
///
3-
/// If multiple subsequences with the longest possible subsequence length can be found, the
4-
/// subsequence which appeared first will be returned (see `test_example_1`).
5-
///
6-
/// Inspired by [this LeetCode problem](https://leetcode.com/problems/longest-increasing-subsequence/).
1+
//! Longest Increasing Subsequence
2+
//!
3+
//! # Algorithm
4+
//!
5+
//! The algorithm uses dynamic programming to efficiently find the longest increasing subsequence.
6+
//! It maintains a sequence of increasing elements while iterating through the input array.
7+
//! For each element in the input array, it compares the element with the last element in the increasing sequence.
8+
//! If the current element is greater, it extends the sequence. If not, it finds the correct position to replace a previous element to maintain the increasing order.
9+
//!
10+
//! The result is the longest increasing subsequence found in the input array. If multiple subsequences with the same length are possible, the one that appeared first in the input array is returned.
11+
//!
12+
//! # Generic Type
13+
//!
14+
//! This function is generic over the type of elements in the input array and requires the `Ord` trait to be implemented for the elements.
15+
//!
16+
//! # Panics
17+
//!
18+
//! This function does not panic and will return an empty vector if the input array is empty.
19+
//!
20+
//! # Inspired by
21+
//!
22+
//! This function is inspired by the [Longest Increasing Subsequence problem on LeetCode](https://leetcode.com/problems/longest-increasing-subsequence/).
23+
724
pub fn longest_increasing_subsequence<T: Ord + Clone>(input_array: &[T]) -> Vec<T> {
825
let n = input_array.len();
926
if n <= 1 {

0 commit comments

Comments
 (0)