Class Solution
java.lang.Object
g2401_2500.s2407_longest_increasing_subsequence_ii.Solution
2407 - Longest Increasing Subsequence II.<p>Hard</p>
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
<p>Find the longest subsequence of <code>nums</code> that meets the following requirements:</p>
<ul>
<li>The subsequence is <strong>strictly increasing</strong> and</li>
<li>The difference between adjacent elements in the subsequence is <strong>at most</strong> <code>k</code>.</li>
</ul>
<p>Return <em>the length of the <strong>longest</strong> <strong>subsequence</strong> that meets the requirements.</em></p>
<p>A <strong>subsequence</strong> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> nums = [4,2,1,4,3,4,5,8,15], k = 3</p>
<p><strong>Output:</strong> 5</p>
<p><strong>Explanation:</strong></p>
<p>The longest subsequence that meets the requirements is [1,3,4,5,8].</p>
<p>The subsequence has a length of 5, so we return 5.</p>
<p>Note that the subsequence [1,3,4,5,8,15] does not meet the requirements because 15 - 8 = 7 is larger than 3.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums = [7,4,5,1,8,12,4,7], k = 5</p>
<p><strong>Output:</strong> 4</p>
<p><strong>Explanation:</strong></p>
<p>The longest subsequence that meets the requirements is [4,5,8,12].</p>
<p>The subsequence has a length of 4, so we return 4.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> nums = [1,5], k = 1</p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong> The longest subsequence that meets the requirements is [1]. The subsequence has a length of 1, so we return 1.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], k <= 10<sup>5</sup></code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
lengthOfLIS
public int lengthOfLIS(int[] nums, int k)
-