#300
Medium Algorithms Longest increasing subsequence
Array Binary Search Dynamic Programming
59.1% acceptance
Jan 12, 2026
22660
511
Given an integer array nums, return the length of the longest strictly increasing subsequence.
Solution
Rust
Time O(n log n)
Space O(n)
impl Solution {
pub fn length_of_lis(nums: Vec<i32>) -> i32 {
let mut dp = Vec::new();
for &num in &nums {
let pos = dp.binary_search(&num).unwrap_or_else(|x| x);
if pos == dp.len() {
dp.push(num);
} else {
dp[pos] = num;
}
}
dp.len() as i32
}
}