From 33dbe9bc568f6a0b6754ffbae56e3d45e8c010bb Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 8 Aug 2022 21:58:30 +0200 Subject: [PATCH] =?UTF-8?q?problems:=20add=20=E2=80=9ELongest=20Increasing?= =?UTF-8?q?=20Subsequence=E2=80=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- problems/longest-increasing-subsequence.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 problems/longest-increasing-subsequence.rs diff --git a/problems/longest-increasing-subsequence.rs b/problems/longest-increasing-subsequence.rs new file mode 100644 index 0000000..a269416 --- /dev/null +++ b/problems/longest-increasing-subsequence.rs @@ -0,0 +1,18 @@ +use std::cmp::max; + +impl Solution { + pub fn length_of_lis(nums: Vec) -> i32 { + let mut dp = Vec::::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() + } +}