GoConqr has a new look! Let us know what you think.

Flashcards

Merge Intervals

Suhas S English 1 card

Cards in this deck (1)

  1. 1Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18].private class IntervalComparator implements Comparator {
    @Override
    public int compare(Interval a, Interval b) {
    return a.start < b.start ? -1 : a.start == b.start ? 0 : 1;
    }
    }
    public List merge(List intervals) {
    Collections.sort(intervals, new IntervalComparator());
    LinkedList mergedList = new LinkedList();
    for(Interval interval : intervals){
    if(mergedList.isEmpty() || mergedList.getLast().end < interval.start ){
    mergedList.add(interval);
    }else{
    mergedList.getLast().end = Math.max( mergedList.getLast().end , interval.end);
    }
    }
    return mergedList;
    }

Information

Description
Flashcards on Merge Intervals, created by Suhas S on 12/02/2018.
Created by
Suhas S
Views
14
Tags
arrays
Rating

Feedback

What is it about?

What do you think of the new look?

How do you like it?