Longest Increasing Subsequence
Find how many numbers, kept in their order, can be picked from an array so that each one is larger than the last.
Given an array of integers nums
Example 1
Output: 4
Explanation: one longest choice is [2, 3, 7, 101].
Example 2
Output: 4
Example 3
Output: 1
The Idea
Read the numbers of Example 1 from left to right, like a row of steps, and keep only steps that go up. Keeping 2, 5, 7 and 101 works, and so does 2, 3, 7 and 18. Keeping 10 and then 101 works too, but that climb stops after two steps. No choice of five numbers keeps going up, so the answer is 4.
Example 3 shows why the word strictly matters. In [7, 7, 7, 7]
Each number can be kept or dropped, so eight numbers give 28 = 256 different subsequences, and every extra number doubles that count. How can we find the longest climb without trying them all?
Ask a smaller question for each position instead: how long is the longest increasing subsequence that ends exactly at this number? Take the 7 at index 5, counting positions from 0. In a climb that ends at 7, the number just before 7 is earlier and smaller, so it is 2, 5 or 3. If we already know the longest climb that ends at each of those three, 7 simply adds one step to the best of them. Look at the picture below.
The row labelled "nums" is Example 1, and under each number, the row labelled "dp: longest ending here" holds the answer to the smaller question. The teal boxes are the smaller values before 7: 2, 5 and 3, with 1, 2 and 2 below them. The best is 2, from 5 or 3, so the orange box under 7 holds 1 + 2 = 3, the climb 2, 3, 7.
Further right, 101 builds on that 3 and gets 4, and so does 18. The longest climb can end at any number, so the answer to the whole problem is the largest entry in the row, 4.
So each entry is worked out from entries to its left that are already known. Here the answers for all eight positions go in one row named dpdp[i]nums[i]
Solution Approach
Turning that idea into code means settling three things: what each entry of dp
The starting value of each entry
What is dp[i]nums[i]
Growing an entry from earlier entries
Which earlier entries can nums[i]jjinums[j] < nums[i]nums[i]nums[j]dp[j] + 1dp[i]dp[j] + 1
Filling the row from left to right means every dp[j]dp[i]<=
Where the answer is
Why not simply return the last entry? The longest climb does not have to end at the last number. In [1, 2, 3, 0]dp
Optional: a faster method with the smallest tails
Is there a way to avoid looking back at every earlier number? Keep a sorted list called tailskk + 1
For each new number, find the first entry in tails
Next, 7 and 101 go on the end, [2, 3, 7, 101], and 18 replaces 101, giving [2, 3, 7, 18]. The length of tails
Java Solution
The method endingAtlengthOfLIS
- First,
creates the tableendingAt withdp , one entry per number.new int[nums.length] - A loop moves
from left to right and setsi , the climb ofdp[i] = 1 on its own.nums[i] - Inside, a second loop moves
over the positions beforej . Wheni ,nums[j] < nums[i] becomesdp[i] if that is larger.dp[j] + 1 - When both loops are done,
returns the filledendingAt .dp walks over that table, keeps the largest entry inlengthOfLIS , which starts at 0, and returnsbest .best
The main
import java.util.Arrays;
public class LongestIncreasingSubsequence {
// dp[i] = length of the longest increasing subsequence that ends at nums[i]
public static int[] endingAt(int[] nums) {
int[] dp = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
dp[i] = 1; // nums[i] on its own
for (int j = 0; j < i; j++) {
if (nums[j] < nums[i]) { // nums[i] can follow nums[j]
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}
return dp;
}
public static int lengthOfLIS(int[] nums) {
int best = 0;
for (int length : endingAt(nums)) {
best = Math.max(best, length); // the longest may end anywhere
}
return best;
}
static void show(int[] nums) {
System.out.println("nums " + Arrays.toString(nums) + ", dp "
+ Arrays.toString(endingAt(nums)) + ", answer " + lengthOfLIS(nums));
}
public static void main(String[] args) {
show(new int[] {10, 9, 2, 5, 3, 7, 101, 18});
show(new int[] {0, 1, 0, 3, 2, 3});
show(new int[] {7, 7, 7, 7});
show(new int[] {5, 4, 3, 2, 1});
show(new int[] {1, 2, 3, 0});
show(new int[] {});
}
}
Running it prints:
nums [10, 9, 2, 5, 3, 7, 101, 18], dp [1, 1, 1, 2, 2, 3, 4, 4], answer 4
nums [0, 1, 0, 3, 2, 3], dp [1, 2, 1, 3, 3, 4], answer 4
nums [7, 7, 7, 7], dp [1, 1, 1, 1], answer 1
nums [5, 4, 3, 2, 1], dp [1, 1, 1, 1, 1], answer 1
nums [1, 2, 3, 0], dp [1, 2, 3, 1], answer 3
nums [], dp [], answer 0
The table for Example 1, [1, 1, 1, 2, 2, 3, 4, 4]
For [7, 7, 7, 7]nums[j] < nums[i]<=[5, 4, 3, 2, 1]
Look at the line for [1, 2, 3, 0]best
Complexity
The work is the two loops that fill dp
- Time: O(n²), because each of the n numbers looks back at every number before it, about n²/2 comparisons, while the pass for the largest entry adds only n steps.
- Space: O(n), because
stores one climb length for each of the n numbers.dp
The method with tails