300. Longest Increasing Subsequence
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the length of the longest strictly increasing subsequence (LIS) within a given array of numbers. A subsequence is a sequence that can be derived from the array by deleting some or no elements without changing the order of theremaining elements. A strictly increasing subsequence is one where each element is strictly greater than the one preceding it.
Solution
Longest Increasing Subsequence in Python
import bisect
class Solution:
def lengthOfLIS(self, nums: List[int]) -> int:
# tails[i] is the smallest tail of all increasing subsequences of length i+1.
tails = []
for num in nums:
# Find the insertion point for num in tails to maintain sorted order.
# bisect_left finds the index of the first element >= num.
i = bisect.bisect_left(tails, num)
# If i is at the end, it means num is greater than all elements in tails.
# We can extend the longest increasing subsequence found so far.
if i == len(tails):
tails.append(num)
else:
# If we found an element, it means we can form an increasing subsequence
# of the same length but with a smaller tail (num).
# This provides a better opportunity for future extensions.
tails[i] = num
# The length of the tails list is the length of the LIS.
return len(tails)
Longest Increasing Subsequence in Java
class Solution {
public int lengthOfLIS(int[] nums) {
// tails[i] is the smallest tail of all increasing subsequences of length i+1.
// We use an array and a pointer `len` to represent our `tails` list.
int[] tails = new int[nums.length];
int len = 0;
for (int num : nums) {
// Binary search to find the insertion point for num in tails.
// We are looking for the first element >= num.
int i = 0, j = len;
while (i < j) {
int m = i + (j - i) / 2;
if (tails[m] < num) {
i = m + 1;
} else {
j = m;
}
}
// `i` is the insertion point. It's the index of the first element >= num.
// Or it's `len` if `num` is greater than all elements in `tails`.
tails[i] = num;
// If the insertion point is at the end of our current LIS tails,
// it means we have extended the LIS, so we increment its length.
if (i == len) {
len++;
}
}
// The length of the `tails` list is the length of the LIS.
return len;
}
}
Longest Increasing Subsequence in C++
class Solution {
public:
int lengthOfLIS(vector<int>& nums) {
// tails[i] is the smallest tail of all increasing subsequences of length i+1
vector<int> tails;
for (int num : nums) {
// Use binary search (lower_bound) to find the first element in tails
// that is not less than (i.e., greater than or equal to) num.
auto it = lower_bound(tails.begin(), tails.end(), num);
// If num is greater than all elements in tails,
// it extends the longest subsequence found so far.
if (it == tails.end()) {
tails.push_back(num);
} else {
// If we found an element, it means we can form an increasing subsequence
// of the same length but with a smaller tail (num).
// This provides a better opportunity for future extensions.
*it = num;
}
}
// The size of the tails vector is the length of the LIS.
return tails.size();
}
};
Longest Increasing Subsequence in TypeScript
function lengthOfLIS(nums: number[]): number {
// tails[i] is the smallest tail of all increasing subsequences of length i+1.
const tails: number[] = [];
for (const num of nums) {
// Binary search to find the insertion point for num in tails.
// We are looking for the first element >= num.
let low = 0;
let high = tails.length;
while (low < high) {
const mid = Math.floor(low + (high - low) / 2);
if (tails[mid] < num) {
low = mid + 1;
} else {
high = mid;
}
}
// `low` is the insertion point.
// If low is at the end, it means num is greater than all elements in tails.
// We can extend the longest increasing subsequence found so far.
if (low === tails.length) {
tails.push(num);
} else {
// If we found an element, it means we can form an increasing subsequence
// of the same length but with a smaller tail (num).
// This provides a better opportunity for future extensions.
tails[low] = num;
}
}
// The length of the tails array is the length of the LIS.
return tails.length;
}
More Medium LeetCode answers
- 102. Binary Tree Level Order Traversal
- 103. Binary Tree Zigzag Level Order Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 107. Binary Tree Level Order Traversal II
- 113. Path Sum II
- 114. Flatten Binary Tree to Linked List
- 116. Populating Next Right Pointers in Each Node
- 117. Populating Next Right Pointers in Each Node II
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.