You are given an array of n pairs pairs where pairs[i] = [lefti, righti] and lefti < righti.
A pair p2 = [c, d] follows a pair p1 = [a, b] if b < c. A chain of pairs can be formed in this fashion.
Return the length longest chain which can be formed.
You do not need to use up all the given intervals. You can select pairs in any order.
Example 1:
Input: pairs = [[1,2],[2,3],[3,4]] Output: 2 Explanation: The longest chain is [1,2] -> [3,4].
Example 2:
Input: pairs = [[1,2],[7,8],[4,5]] Output: 3 Explanation: The longest chain is [1,2] -> [4,5] -> [7,8].
Constraints:
n == pairs.length1 <= n <= 1000-1000 <= lefti < righti <= 1000When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We want to find the longest chain of pairs that follow a specific ordering rule. The brute force approach involves checking every possible combination of pairs to see which one creates the longest valid chain. Essentially, we try everything.
Here's how the algorithm would work step-by-step:
def find_maximum_pair_chain_length_brute_force(pairs):
maximum_chain_length = 0
for starting_pair_index in range(len(pairs)):
current_chain_length = find_chain_length(
pairs, starting_pair_index, [starting_pair_index]
)
maximum_chain_length = max(
maximum_chain_length, current_chain_length
)
return maximum_chain_length
def find_chain_length(pairs, last_pair_index, current_chain):
maximum_length = len(current_chain)
for next_pair_index in range(len(pairs)):
# Ensure we dont reuse pairs in the chain
if next_pair_index not in current_chain:
if can_append(pairs[last_pair_index], pairs[next_pair_index]):
# Extend the chain recursively if possible
extended_chain = current_chain + [next_pair_index]
chain_length = find_chain_length(
pairs, next_pair_index, extended_chain
)
maximum_length = max(maximum_length, chain_length)
return maximum_length
def can_append(first_pair, second_pair):
# Check if pairs can be chained
return first_pair[1] < second_pair[0]The goal is to find the longest possible chain of pairs where each pair follows the previous one. The key idea is to sort the pairs cleverly and then greedily pick the pairs that extend the chain as far as possible.
Here's how the algorithm would work step-by-step:
def find_longest_chain(pairs):
# Sort pairs based on the ending value.
pairs.sort(key=lambda pair: pair[1])
current_chain_length = 0
last_pair_end = float('-inf')
for pair in pairs:
# Check if the current pair can extend the chain.
if pair[0] > last_pair_end:
#The current pair extends the chain.
current_chain_length += 1
last_pair_end = pair[1]
return current_chain_length| Case | How to Handle |
|---|---|
| Null or empty input array | Return 0, as there are no pairs to form a chain. |
| Input array with only one pair | Return 1, as a single pair is a chain of length 1. |
| Pairs are already sorted by their second element (optimal ordering) | The solution should still correctly identify the maximum chain length without unnecessary computations if it relies on sorting. |
| All pairs overlap (no chain longer than 1 possible) | The algorithm should correctly return 1. |
| Pairs with very large or very small integer values | Ensure the chosen data type (e.g., int) can accommodate the range of values without overflow or underflow during comparisons. |
| Duplicate pairs in the input | The sorting and comparison logic should handle duplicate pairs correctly, treating them as distinct elements in the decision of chain extension. |
| Maximum sized input array (close to memory limits) | Ensure that the space complexity of the solution remains within acceptable bounds, especially if sorting or using auxiliary data structures. |
| Pairs sorted in reverse order requiring full traversal and logic | The algorithm should perform correctly even if the pairs are initially in the least optimal order. |