algo/rust/42_dynamic_programming/longest_increasing_subsequence.rs
caitlin.gao 3e7b14e83e feat(geektime_algo): add 42 dynamic programming
edit_distance, longest_increasing_subsequence
2019-09-05 13:55:55 +08:00

23 lines
637 B
Rust

// leetcode 300 [longest_increasing_subsequence](https://leetcode.com/problems/longest-increasing-subsequence)
fn longest_increasing_subsequence(nums: Vec<i32>) -> i32 {
if nums.len() <= 1 { return nums.len() as i32; }
let mut dp = vec![1; nums.len()];
let mut max_list = 1;
for i in 0..nums.len() {
for j in 0..i {
if nums[i] > nums[j] {
dp[i] = dp[i].max(dp[j]+1);
}
}
max_list = max_list.max(dp[i]);
}
max_list
}
fn main() {
let nums = vec![2, 9, 3, 6, 5, 1, 7];
let m = longest_increasing_subsequence(nums);
println!("{:?}", m);
}