Leetcode Problem 2407. Longest Increasing Subsequence II
2407. Longest Increasing Subsequence II
Leetcode Solutions
Segment Tree Approach for Longest Increasing Subsequence with Maximum Difference Constraint
Initialize a Segment Tree that can cover the range from 0 to the maximum value in nums.
Iterate over each number num in nums.
Perform a range query on the Segment Tree for the range [max(0, num - k), num - 1] to find the maximum length of a subsequence that can be extended by num.
Update the value at num in the Segment Tree with the maximum length found plus one.
After processing all numbers, the maximum value in the Segment Tree will be the length of the longest increasing subsequence that satisfies the difference constraint.
Dynamic Programming Approach for Longest Increasing Subsequence with Maximum Difference Constraint