646. Maximum Length of Pair Chain
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 maximum number of pairs that can form a "chain". A pair [c, d] can follow [a, b] if b < c. This is a variation of the classic Activity Selection Problem, where we want to select the maximum number of non-overlapping activities. Here, "non-overlapping" is defined by the specific b < c condition.
Solution
Maximum Length of Pair Chain in Python
class Solution:
def findLongestChain(self, pairs: List[List[int]]) -> int:
# Handle the edge case of an empty input list.
if not pairs:
return 0
# Sort pairs based on the second element (end of the pair) in ascending order.
# This is the key greedy choice.
pairs.sort(key=lambda x: x[1])
# The first pair in the sorted list is always part of our initial chain.
chain_length = 1
current_end = pairs[0][1]
# Iterate through the rest of the sorted pairs.
for i in range(1, len(pairs)):
# If the start of the current pair is strictly greater than the end of the
# last pair in our chain, we can extend the chain.
if pairs[i][0] > current_end:
chain_length += 1
# Update the end of the chain to the end of the current pair.
current_end = pairs[i][1]
return chain_length
Maximum Length of Pair Chain in Java
class Solution {
public int findLongestChain(int[][] pairs) {
// Handle the edge case of an empty or null input array.
if (pairs == null || pairs.length == 0) {
return 0;
}
// Sort pairs based on the second element (end of the pair) in ascending order.
// This is the key greedy choice.
Arrays.sort(pairs, Comparator.comparingInt(a -> a[1]));
// The first pair in the sorted list is always part of our initial chain.
int chainLength = 1;
int currentEnd = pairs[0][1];
// Iterate through the rest of the sorted pairs.
for (int i = 1; i < pairs.length; i++) {
// If the start of the current pair is strictly greater than the end of the
// last pair in our chain, we can extend the chain.
if (pairs[i][0] > currentEnd) {
chainLength++;
// Update the end of the chain to the end of the current pair.
currentEnd = pairs[i][1];
}
}
return chainLength;
}
}
Maximum Length of Pair Chain in C++
class Solution {
public:
int findLongestChain(vector<vector<int>>& pairs) {
// Handle the edge case of an empty input array.
if (pairs.empty()) {
return 0;
}
// Sort pairs based on the second element (end of the pair) in ascending order.
// This is the key greedy choice.
std::sort(pairs.begin(), pairs.end(), [](const vector<int>& a, const vector<int>& b) {
return a[1] < b[1];
});
// The first pair in the sorted list is always part of our initial chain.
int chainLength = 1;
int currentEnd = pairs[0][1];
// Iterate through the rest of the sorted pairs.
for (size_t i = 1; i < pairs.size(); ++i) {
// If the start of the current pair is strictly greater than the end of the
// last pair in our chain, we can extend the chain.
if (pairs[i][0] > currentEnd) {
chainLength++;
// Update the end of the chain to the end of the current pair.
currentEnd = pairs[i][1];
}
}
return chainLength;
}
};
Maximum Length of Pair Chain in TypeScript
function findLongestChain(pairs: number[][]): number {
// Handle the edge case of an empty or null input array.
if (!pairs || pairs.length === 0) {
return 0;
}
// Sort pairs based on the second element (end of the pair) in ascending order.
// This is the key greedy choice.
pairs.sort((a, b) => a[1] - b[1]);
// The first pair in the sorted list is always part of our initial chain.
let chainLength = 1;
let currentEnd = pairs[0][1];
// Iterate through the rest of the sorted pairs.
for (let i = 1; i < pairs.length; i++) {
// If the start of the current pair is strictly greater than the end of the
// last pair in our chain, we can extend the chain.
if (pairs[i][0] > currentEnd) {
chainLength++;
// Update the end of the chain to the end of the current pair.
currentEnd = pairs[i][1];
}
}
return chainLength;
};
More Medium LeetCode answers
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.