Skip to content

Commit 8d528a1

Browse files
committed
src/bin/longest-increasing-subsequence.rs
1 parent 5f70a58 commit 8d528a1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
fn main() {
2+
println!("{}", Solution::length_of_lis(vec![10, 9, 2, 5, 3, 7, 101, 18]));
3+
println!("{}", Solution::length_of_lis(vec![0, 1, 0, 3, 2, 3]));
4+
println!("{}", Solution::length_of_lis(vec![7, 7, 7, 7, 7, 7, 7]));
5+
}
6+
7+
struct Solution;
8+
9+
impl Solution {
10+
pub fn length_of_lis(nums: Vec<i32>) -> i32 {
11+
let mut v = vec![1; nums.len()];
12+
13+
for i in 1..nums.len() {
14+
for j in 0..i {
15+
if nums[j] < nums[i] {
16+
v[i] = v[i].max(v[j] + 1);
17+
}
18+
}
19+
}
20+
21+
v.into_iter().fold(0, |x, y| x.max(y))
22+
}
23+
}

0 commit comments

Comments
 (0)