Atul
BAN USERpublic static int length(Node node, Map<Node,Integer> nodeMap) {
int length = 0;
int tempLeft = 1;
int tempRight = 1;
if(node==null) { return 0;}
if(node.getLeftChild()!=null && node.getLeftChild().getValue() == node.getValue() + 1) {
tempLeft = tempLeft + length(node.getLeftChild(), nodeMap);
} else {
tempLeft =1;
length(node.getLeftChild(), nodeMap);
}
if(node.getRightChild() !=null && node.getRightChild().getValue() == node.getValue() + 1) {
tempRight = tempLeft + length(node.getRightChild(),nodeMap);
} else {
tempRight = 1;
length(node.getRightChild(), nodeMap);
}
length = tempLeft > tempRight ? tempLeft : tempRight;
nodeMap.put(node, length);
return length;
}
//This list will contain the overlapped time slots for all the friends. Initially this list will be //empty.
List<TimeSlot> overlappedTimeSlots = new ArrayList<TimeSlot>()
for(String friend : friendList) {
List<TimeSlot> list = getTimeSlots(friend);
overlappedTimeSlots = getOverlappedTimeSlots(overlappedTimeSlots , list);
}
//return the sub list of overlappedTimeSlots that contain 3 elements.
//getOverlappedTimeSlots will return the list of overlapped time.
//e.g Friend A has (5-6) (9-12) and Friend B has (5,6) and (9,10)
//then getOverlappedTimeSlots will return (5,6) and (9,10)
public static Node<Integer> merge(Node<Integer> n1 , Node<Integer> n2) {
Node<Integer> head = new Node<Integer>();
Node<Integer> node = head;
if(n1==null && n2==null) return null;
if(n1==null && n2!=null) return n2;
if(n1!=null && n2==null) return n1;
while(n1!=null && n2!=null) {
if(n1.get() <=n2.get()) {
Node tmpNode = new Node(n1.get());
node.setNext(tmpNode);
node = tmpNode;
System.out.println(n1.get());
n1 = n1.getNext();
} else {
Node tmpNode = new Node(n2.get());
node.setNext(tmpNode);
node = tmpNode;
System.out.println(n2.get());
n2 = n2.getNext();
}
}
while(n1!=null) {
node.set(n1.get());
System.out.println(n1.get());
n1 = n1.getNext();
}
while(n2!=null) {
Node tmpNode = new Node(n2.get());
node.setNext(tmpNode);
node = tmpNode;
System.out.println(n2.get());
n2 = n2.getNext();
}
return head.getNext();
}
I think it should be Min Heap and this will change your algorithm a bit.
- Atul April 28, 2015