1-D Dynamic Programming Increasing subsequences
529. Print a Longest Increasing Subsequence
You are given an array of integers nums. Return one of its longest strictly increasing subsequences, as the
list of its values in order.
A subsequence keeps some of the elements, in their original order, and drops the rest. The kept elements do not have to be next to each other. It is strictly increasing when every kept element is bigger than the one kept before it, so two equal values can never both be in it.
Often several longest increasing subsequences exist, and any of them is correct. When the list you return is a
strictly increasing subsequence of nums, the result shows its length, as an increasing subsequence of length 4.
Otherwise it shows the first thing wrong with it, such as answer[2] = 3 is not bigger than answer[1] = 3.
Positions count from 0.
Example 1
Input: nums = [4, 10, 1, 6, 2, 7, 3, 9]
[1,2,3,9]Explanation: The values 1, 2, 3 and 9 sit at indexes 2, 4, 6 and 7, and each is bigger than the one before. [4,6,7,9]
is also correct. No increasing subsequence has 5 elements.
Example 2
Input: nums = [3, -5, -5, 0, 0, -2]
[-5,-2]Explanation: Equal values cannot both be kept, so only one -5 and one 0 can be used. [-5,0] is also correct.
Example 3
Input: nums = [1000000000, 0, -1000000000]
[-1000000000]Explanation: Each value is smaller than the one before it, so a longest increasing subsequence holds a single value. Any one of the three is correct.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ nums.length ≤ 105-109 ≤ nums[i] ≤ 109
Clarifying questions
What you would ask an interviewer before writing any code.