Skip to content

Commit 72ab49a

Browse files
committed
8/20/2020
1 parent 0eb1dcd commit 72ab49a

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn length_of_lis(nums: Vec<i32>) -> i32 {
5+
let n: usize = nums.len();
6+
if n == 0 {
7+
return 0;
8+
}
9+
let mut dp: Vec<i32> = vec![1; n];
10+
let mut max_val: i32 = 1;
11+
12+
for i in 1..n {
13+
let mut max_i: i32 = 0;
14+
for j in 0..i {
15+
if nums[i] > nums[j] {
16+
max_i = max_i.max(dp[j]);
17+
}
18+
}
19+
dp[i] = max_i + 1;
20+
max_val = max_val.max(dp[i]);
21+
}
22+
max_val
23+
}
24+
}
25+
26+
#[test]
27+
fn test() {
28+
assert_eq!(Solution::length_of_lis(vec![10, 9, 2, 5, 3, 7, 101, 18]), 4);
29+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ mod _0268_missing_number;
122122
//
123123
mod _0283_move_zeroes;
124124
//
125+
mod _0300_longest_increasing_subsequence;
126+
//
125127
mod _0342_power_of_four;
126128
//
127129
mod _0344_reverse_string;

0 commit comments

Comments
 (0)