Skip to content

Commit 4ca1595

Browse files
Maximum Length of Pair Chain - Accepted
1 parent 5411b8b commit 4ca1595

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ My accepted leetcode solutions to some of the common interview problems.
142142
- [Longest Paliandromic Substring](problems/src/dynamic_programming/LongestPaliandromicSubstring.java) (Medium)
143143
- [Longest Palindromic Subsequence](problems/src/dynamic_programming/LongestPalindromicSubsequence.java) (Medium)
144144
- [Maximum Product Subarray](problems/src/dynamic_programming/MaximumProductSubarray.java) (Medium)
145-
- [Min Cost Climbing Stairs](problems/src/dynamic_programming/MinCostClimbingStairs.java) (Medium)
145+
- [Min Cost Climbing Stairs](problems/src/dynamic_programming/MinCostClimbingStairs.java) (Easy)
146146
- [Palindrome Partitioning II](problems/src/dynamic_programming/PalindromePartitioningII.java) (Hard)
147147
- [UniqueBinary Search Trees](problems/src/dynamic_programming/UniqueBinarySearchTrees.java) (Medium)
148148
- [Unique Binary Search Trees II](problems/src/dynamic_programming/UniqueBinarySearchTreesII.java) (Medium)
@@ -180,6 +180,7 @@ My accepted leetcode solutions to some of the common interview problems.
180180
- [Minimum Number of Arrows to Burst Balloons](problems/src/greedy/BurstBalloons.java) (Medium)
181181
- [Queue Reconstruction By Height](problems/src/greedy/QueueReconstructionByHeight.java) (Medium)
182182
- [Task Scheduler](problems/src/greedy/TaskScheduler.java) (Medium)
183+
- [Maximum Length of Pair Chain](problems/src/greedy/MaximumLengthOfPairChain.java) (Medium)
183184

184185
#### [Hashing](problems/src/hashing)
185186

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package greedy;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Created by gouthamvidyapradhan on 01/05/2018.
7+
* You are given n pairs of numbers. In every pair, the first number is always smaller than the second number.
8+
9+
Now, we define a pair (c, d) can follow another pair (a, b) if and only if b < c.
10+
Chain of pairs can be formed in this fashion.
11+
12+
Given a set of pairs, find the length longest chain which can be formed. You needn't use up all the given pairs.
13+
You can select pairs in any order.
14+
15+
Example 1:
16+
Input: [[1,2], [2,3], [3,4]]
17+
Output: 2
18+
Explanation: The longest chain is [1,2] -> [3,4]
19+
Note:
20+
The number of given pairs will be in the range [1, 1000].
21+
22+
Solution: O(N log N) sort the pairs with ending interval (greedy sort) and try to accommodate as many pairs
23+
as possible. If any current pair violates the chaining condition (b < c) then, ignore that particular pair.
24+
*/
25+
public class MaximumLengthOfPairChain {
26+
27+
/**
28+
* Main method
29+
* @param args
30+
* @throws Exception
31+
*/
32+
public static void main(String[] args) throws Exception{
33+
int[][] A = {{1,2}, {2,3}, {3,4}};
34+
System.out.println(new MaximumLengthOfPairChain().findLongestChain(A));
35+
}
36+
37+
public int findLongestChain(int[][] pairs) {
38+
Arrays.sort(pairs, (o1, o2) -> o1[1] == o2[1] ? Integer.compare(o1[0], o2[0]) : Integer.compare(o1[1], o2[1]));
39+
int count = 1;
40+
int[] curr = pairs[0];
41+
for(int i = 1; i < pairs.length; i ++){
42+
if(pairs[i][0] > curr[1]){
43+
count++;
44+
curr = pairs[i];
45+
}
46+
}
47+
return count;
48+
}
49+
50+
}

0 commit comments

Comments
 (0)