20240807 Finished

This commit is contained in:
jackfiled 2024-08-07 11:58:25 +08:00
parent c7159161af
commit 21cf6e05d6
2 changed files with 57 additions and 1 deletions

View File

@ -197,3 +197,4 @@ mod p149_max_points_on_a_line;
mod p70_climbing_stairs; mod p70_climbing_stairs;
mod p198_house_robber; mod p198_house_robber;
mod p139_word_break; mod p139_word_break;
mod p300_longest_increasing_subsequence;

View File

@ -0,0 +1,55 @@
/**
* [300] Longest Increasing Subsequence
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn length_of_lis(nums: Vec<i32>) -> i32 {
let n = nums.len();
if n == 0 {
return 0;
}
let mut length = 1;
let mut dp = vec![0;n + 1];
dp[length] = nums[0];
for i in 1..n {
if nums[i] > dp[length] {
length += 1;
dp[length] = nums[i];
} else {
// 二分查找dp中小于nums[i]的位置
let (mut left, mut right, mut pos) = (1, length, 0);
while left <= right {
let middle = (right - left) / 2 + left;
if dp[middle] < nums[i] {
pos = middle;
left = middle + 1;
} else {
right = middle - 1;
}
}
dp[pos + 1] = nums[i];
}
}
length as i32
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_300() {
}
}