-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Expand file tree
/
Copy pathMaximumLengthChainPair.java
More file actions
64 lines (56 loc) · 1.55 KB
/
Copy pathMaximumLengthChainPair.java
File metadata and controls
64 lines (56 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.interview.dynamic;
import java.util.Arrays;
/**
* http://www.geeksforgeeks.org/dynamic-programming-set-20-maximum-length-chain-of-pairs/
*/
public class MaximumLengthChainPair {
static class Pair implements Comparable<Pair>{
public Pair(int a,int b){
this.a = a;
this.b = b;
}
int a;
int b;
@Override
public int compareTo(Pair o) {
if(this.a <= o.a){
return -1;
}else{
return 1;
}
}
}
public int maxLength(Pair[] arr){
Arrays.sort(arr);
int T[] = new int[arr.length+1];
for(int i=0; i < T.length; i++){
T[i] = 1;
}
for(int i=2; i < T.length; i++){
for(int j=1; j < i; j++){
if(arr[j-1].b < arr[i-1].a){
T[i] = Math.max(T[j] + 1, T[i]);
}
}
}
int max =0 ;
for(int i=1; i <T.length; i++){
if(max < T[i]){
max = T[i];
}
}
return max;
}
public static void main(String args[]){
Pair p = new Pair(5,7);
Pair p1 = new Pair(2,3);
Pair p2 = new Pair(2,6);
Pair p3 = new Pair(9,11);
Pair p4 = new Pair(8,10);
Pair p5 = new Pair(11,14);
Pair[] arr = {p,p1,p2,p3,p4,p5};
MaximumLengthChainPair mlcp = new MaximumLengthChainPair();
int r = mlcp.maxLength(arr);
System.out.print(r);
}
}