Maximum Length of Pair Chain - Complete Solution Guide
Maximum Length of Pair Chain is LeetCode problem 646, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an array of n pairs pairs where pairs[i] = [left i , right i ] and left i < right i . 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]] Out
Detailed Explanation
The problem asks us to find the maximum length of a chain of pairs. A pair `p2 = [c, d]` follows a pair `p1 = [a, b]` if `b < c`. We are given an array of pairs `pairs` where each `pairs[i] = [left_i, right_i]` and `left_i < right_i`. The goal is to find the longest possible sequence of pairs where each pair follows the previous one.
Solution Approach
The solution uses a greedy approach. First, the pairs are sorted in ascending order based on their end times. Then, we iterate through the sorted pairs. If the start time of the current pair is greater than the end time of the last pair added to the chain, we add the current pair to the chain and update the end time of the last pair added to the chain. This process continues until all pairs have been considered. The final chain length is the maximum length of the pair chain.
Step-by-Step Algorithm
- Step 1: Sort the input `pairs` array in ascending order based on the second element (end time) of each pair.
- Step 2: Initialize `chain_length` to 0 and `current_end` to negative infinity (or the smallest possible integer value). This represents the end time of the last pair added to the chain.
- Step 3: Iterate through the sorted `pairs` array. For each pair `[start, end]`, check if `start > current_end`.
- Step 4: If `start > current_end`, it means the current pair can be added to the chain without violating the 'follows' condition. Increment `chain_length` by 1 and update `current_end` to `end`.
- Step 5: After iterating through all the pairs, return the final `chain_length`.
Key Insights
- Insight 1: Sorting the pairs based on their end times (right_i) is crucial. This allows us to greedily select pairs to form the longest chain.
- Insight 2: A greedy approach is applicable here. After sorting by end times, we pick the pair with the smallest end time that is still greater than the end time of the previous pair in the chain.
- Insight 3: The key property used in the greedy approach is that picking the pair with the smallest end time allows for the possibility of fitting more pairs into the chain later on.
Complexity Analysis
Time Complexity: O(n log n)
Space Complexity: O(1)
Topics
This problem involves: Array, Dynamic Programming, Greedy, Sorting.
Companies
Asked at: Swiggy.