problems: add „Longest Increasing Subsequence“
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
aba5f5f595
commit
33dbe9bc56
1 changed files with 18 additions and 0 deletions
18
problems/longest-increasing-subsequence.rs
Normal file
18
problems/longest-increasing-subsequence.rs
Normal file
|
@ -0,0 +1,18 @@
|
|||
use std::cmp::max;
|
||||
|
||||
impl Solution {
|
||||
pub fn length_of_lis(nums: Vec<i32>) -> i32 {
|
||||
let mut dp = Vec::<i32>::new();
|
||||
dp.resize(nums.len(), 1);
|
||||
|
||||
for i in 1..nums.len() {
|
||||
for j in 0..i {
|
||||
if nums[i] > nums[j] {
|
||||
dp[i] = max(dp[i], dp[j] + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*dp.iter().max().unwrap()
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue