-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest_increasing_subsequence.rs
59 lines (50 loc) · 1.4 KB
/
longest_increasing_subsequence.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::collections::HashMap;
/// Given an integer array `nums`, return the length of the longest strictly
/// increasing subsequence.
struct Solution;
impl Solution {
pub fn length_of_lis(nums: Vec<i32>) -> i32 {
let mut lengths = HashMap::new();
let mut result = 0;
for i in 0..nums.len() {
result = result.max(Self::worker(i, &nums, &mut lengths));
}
result
}
fn worker(i: usize, nums: &[i32], lengths: &mut HashMap<usize, i32>) -> i32 {
if lengths.contains_key(&i) {
lengths[&i]
} else {
let mut result = 1;
for j in 0..i {
if nums[i] > nums[j] {
result = result.max(Self::worker(j, nums, lengths) + 1);
}
}
lengths.insert(i, result);
result
}
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn example_1() {
let nums = vec![10, 9, 2, 5, 3, 7, 101, 18];
let result = Solution::length_of_lis(nums);
assert_eq!(result, 4);
}
#[test]
fn example_2() {
let nums = vec![0, 1, 0, 3, 2, 3];
let result = Solution::length_of_lis(nums);
assert_eq!(result, 4);
}
#[test]
fn example_3() {
let nums = vec![7, 7, 7, 7, 7, 7, 7];
let result = Solution::length_of_lis(nums);
assert_eq!(result, 1);
}
}