medium Longest Increasing Subsequence
Problem Statement
Given a number sequence, find the length of its Longest Increasing Subsequence (LIS). In an increasing subsequence, all the elements are in increasing order (from lowest to highest).
Example 1:
Input: {4,2,3,6,10,1,12}
Output: 5
Explanation: The LIS is {2,3,6,10,12}.
Example 1:
Input: {-4,10,3,7,15}
Output: 4
Explanation: The LIS is {-4,3,7,15}.
Constraints:
1 <= nums.length <= 2500- -104 <= nums[i] <= 104
Try it yourself
Try solving this question here:
✅ Solution Longest Increasing Subsequence
Problem Statement
Given a number sequence, find the length of its Longest Increasing Subsequence (LIS). In an increasing subsequence, all the elements are in increasing order (from lowest to highest).
Example 1:
Input: {4,2,3,6,10,1,12}
Output: 5
Explanation: The LIS is {2,3,6,10,12}.
Example 1:
Input: {-4,10,3,7,15}
Output: 4
Explanation: The LIS is {-4,3,7,15}.
Constraints:
1 <= nums.length <= 2500- -104 <= nums[i] <= 104
Basic Solution
A basic brute-force solution could be to try all the subsequences of the given number sequence. We can process one number at a time, so we have two options at any step:
- If the current number is greater than the previous number that we included, we can increment our count and make a recursive call for the remaining array.
- We can skip the current number to make a recursive call for the remaining array.
The length of the longest increasing subsequence will be the maximum number returned by the two recurse calls from the above two options.
Code
Here is the code:
class Solution {
public int findLISLength(int[] nums) {
return findLISLengthRecursive(nums, 0, -1);
}
private int findLISLengthRecursive(int[] nums, int currentIndex, int previousIndex) {
if(currentIndex == nums.length)
return 0;
// include nums[currentIndex] if it is larger than the last included number
int c1 = 0;
if(previousIndex == -1 || nums[currentIndex] > nums[previousIndex])
c1 = 1 + findLISLengthRecursive(nums, currentIndex+1, currentIndex);
// excluding the number at currentIndex
int c2 = findLISLengthRecursive(nums, currentIndex+1, previousIndex);
return Math.max(c1, c2);
}
public static void main(String[] args) {
Solution lis = new Solution();
int[] nums = {4,2,3,6,10,1,12};
System.out.println(lis.findLISLength(nums));
nums = new int[]{-4,10,3,7,15};
System.out.println(lis.findLISLength(nums));
}
}
The time complexity of the above algorithm is exponential
Top-down Dynamic Programming with Memoization
To overcome the overlapping subproblems, we can use an array to store the already solved subproblems.
The two changing values for our recursive function are the current and the previous index. Therefore, we can store the results of all subproblems in a two-dimensional array. (Another alternative could be to use a hash-table whose key would be a string (currentIndex + "|" + previousIndex)).
Code
Here is the code:
class Solution {
public int findLISLength(int[] nums) {
Integer[][] dp = new Integer[nums.length][nums.length + 1];
return findLISLengthRecursive(dp, nums, 0, -1);
}
private int findLISLengthRecursive(
Integer[][] dp,
int[] nums,
int currentIndex,
int previousIndex
) {
if (currentIndex == nums.length) return 0;
if (dp[currentIndex][previousIndex + 1] == null) {
// include nums[currentIndex] if it is larger than the last included number
int c1 = 0;
if (previousIndex == -1 || nums[currentIndex] > nums[previousIndex]) c1 =
1 + findLISLengthRecursive(dp, nums, currentIndex + 1, currentIndex);
int c2 = findLISLengthRecursive(
dp,
nums,
currentIndex + 1,
previousIndex
);
dp[currentIndex][previousIndex + 1] = Math.max(c1, c2);
}
return dp[currentIndex][previousIndex + 1];
}
public static void main(String[] args) {
Solution lis = new Solution();
int[] nums = { 4, 2, 3, 6, 10, 1, 12 };
System.out.println(lis.findLISLength(nums));
nums = new int[] { -4, 10, 3, 7, 15 };
System.out.println(lis.findLISLength(nums));
}
}
What is the time and space complexity of the above solution? Since our memoization array dp[nums.length()][nums.length()] stores the results for all the subproblems, we can conclude that we will not have more than
The above algorithm will be using
Bottom-up Dynamic Programming
The above algorithm tells us two things:
- If the number at the current index is bigger than the number at the previous index, we increment the count for LIS up to the current index.
- But if there is a bigger LIS without including the number at the current index, we take that.
So we need to find all the increasing subsequences for the number at index 'i', from all the previous numbers (i.e. number till index 'i-1'), to eventually find the longest increasing subsequence.
If 'i' represents the 'currentIndex' and 'j' represents the 'previousIndex', our recursive formula would look like:
if num[i] > num[j] => dp[i] = dp[j] + 1 if there is no bigger LIS for 'i'
Let's draw this visually for {-4,10,3,7,15}. Start with a subsequence of length '1', as every number will be a LIS of length '1':


From the above visualization, we can clearly see that the longest increasing subsequence is of length '4' -- as shown by dp[4].
Code
Here is the code for our bottom-up dynamic programming approach:
class Solution {
public int findLISLength(int[] nums) {
int[] dp = new int[nums.length];
dp[0] = 1;
int maxLength = 1;
for (int i = 1; i < nums.length; i++) {
dp[i] = 1;
for (int j = 0; j < i; j++) if (nums[i] > nums[j] && dp[i] <= dp[j]) {
dp[i] = dp[j] + 1;
maxLength = Math.max(maxLength, dp[i]);
}
}
return maxLength;
}
public static void main(String[] args) {
Solution lis = new Solution();
int[] nums = { 4, 2, 3, 6, 10, 1, 12 };
System.out.println(lis.findLISLength(nums));
nums = new int[] { -4, 10, 3, 7, 15 };
System.out.println(lis.findLISLength(nums));
}
}
The time complexity of the above algorithm is
🤖 Don't fully get this? Learn it with Claude
Stuck on Longest Increasing Subsequence? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Longest Increasing Subsequence** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Longest Increasing Subsequence** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Longest Increasing Subsequence**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Longest Increasing Subsequence**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.