Skip to main content
added 3 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

This code finds the intersections of all overlapping intervals. 

Example: if [0-20], [15-40], and [25-50] are the 3 intervals then the output should be [15-20] and [25-40]. 

I could not find an answer with complexity less than \$O(n^2)\$. Please let me knowsuggest a better solution if itone exists. Additionally, assume the input intervals are sorted by their start times.

public static Set<OverlapCoord> getOverlap(List<Interval> intervalList) {
    if (intervalList == null) {
        throw new NullPointerException("Input list cannot be null.");
    }
    
    final HashSet<OverlapCoord> hashSet = new HashSet<OverlapCoord>();

    for (int i = 0; i < intervalList.size() - 1; i++) {
        final Interval intervali =  intervalList.get(i);
        
        for (int j = 0; j < intervalList.size(); j++) {
            final Interval intervalj = intervalList.get(j);
            
            if (intervalj.getStart() < intervali.getEnd() && intervalj.getEnd() > intervali.getStart() && i != j) {
                hashSet.add(new OverlapCoord(Math.max(intervali.getStart(),intervalj.getStart()), 
                                             Math.min(intervali.getEnd(), intervalj.getEnd())));
            }
        }
    }
    
    return hashSet;
}

This code finds the intersections of all overlapping intervals. Example: if [0-20], [15-40], and [25-50] are the 3 intervals then the output should be [15-20] and [25-40]. I could not find an answer with complexity less than \$O(n^2)\$. Please let me know a better solution if it exists. Additionally, assume the input intervals are sorted by their start times.

public static Set<OverlapCoord> getOverlap(List<Interval> intervalList) {
    if (intervalList == null) {
        throw new NullPointerException("Input list cannot be null.");
    }
    
    final HashSet<OverlapCoord> hashSet = new HashSet<OverlapCoord>();

    for (int i = 0; i < intervalList.size() - 1; i++) {
        final Interval intervali =  intervalList.get(i);
        
        for (int j = 0; j < intervalList.size(); j++) {
            final Interval intervalj = intervalList.get(j);
            
            if (intervalj.getStart() < intervali.getEnd() && intervalj.getEnd() > intervali.getStart() && i != j) {
                hashSet.add(new OverlapCoord(Math.max(intervali.getStart(),intervalj.getStart()), 
                                             Math.min(intervali.getEnd(), intervalj.getEnd())));
            }
        }
    }
    
    return hashSet;
}

This code finds the intersections of all overlapping intervals. 

Example: if [0-20], [15-40], and [25-50] are the 3 intervals then the output should be [15-20] and [25-40]. 

I could not find an answer with complexity less than \$O(n^2)\$. Please suggest a better solution if one exists. Additionally, assume the input intervals are sorted by their start times.

public static Set<OverlapCoord> getOverlap(List<Interval> intervalList) {
    if (intervalList == null) {
        throw new NullPointerException("Input list cannot be null.");
    }
    
    final HashSet<OverlapCoord> hashSet = new HashSet<OverlapCoord>();

    for (int i = 0; i < intervalList.size() - 1; i++) {
        final Interval intervali =  intervalList.get(i);
        
        for (int j = 0; j < intervalList.size(); j++) {
            final Interval intervalj = intervalList.get(j);
            
            if (intervalj.getStart() < intervali.getEnd() && intervalj.getEnd() > intervali.getStart() && i != j) {
                hashSet.add(new OverlapCoord(Math.max(intervali.getStart(),intervalj.getStart()), 
                                             Math.min(intervali.getEnd(), intervalj.getEnd())));
            }
        }
    }
    
    return hashSet;
}
Grammar and title
Source Link
David Harkness
  • 8.9k
  • 1
  • 27
  • 44

Code to find all merging Find intersections of overlapping intervals

This is a code to findfinds the intersections of all overlaps inoverlapping intervals. Example: [0-20] [15-40] [25if -50][0-20], [15-40], and [25-50] are the 3 intervals then the output should be : (15-20),[15-20] and (25-40)[25-40]. I could not find andan answer with complexity lesserless than O(n2)\$O(n^2)\$. Please let me know a better solutionssolution if it exists. Additionally, assume the input isintervals are sorted by starttimetheir start times.

public static Set<OverlapCoord> getOverlap(List<Interval> intervalList) {
    if (intervalList == null) {
        throw new NullPointerException("Input list cannot be null.");
    }
    
    final HashSet<OverlapCoord> hashSet = new HashSet<OverlapCoord>();

    for (int i = 0; i < intervalList.size() - 1; i++) {
        final Interval intervali =  intervalList.get(i);
        
        for (int j = 0; j < intervalList.size(); j++) {
            final Interval intervalj = intervalList.get(j);
            
            if (intervalj.getStart() < intervali.getEnd() && intervalj.getEnd() > intervali.getStart() && i != j) {
                hashSet.add(new OverlapCoord(Math.max(intervali.getStart(),intervalj.getStart()), 
                                             Math.min(intervali.getEnd(), intervalj.getEnd())));
            }
        }
    }
    
    return hashSet;
}

Code to find all merging intervals

This is a code to find all overlaps in intervals. Example: [0-20] [15-40] [25 -50] are 3 intervals then the output should be : (15-20), (25-40). I could not find and answer with complexity lesser than O(n2). Please let me know a better solutions if it exists. Additionally assume input is sorted by starttime.

public static Set<OverlapCoord> getOverlap(List<Interval> intervalList) {
    if (intervalList == null) {
        throw new NullPointerException("Input list cannot be null.");
    }
    
    final HashSet<OverlapCoord> hashSet = new HashSet<OverlapCoord>();

    for (int i = 0; i < intervalList.size() - 1; i++) {
        final Interval intervali =  intervalList.get(i);
        
        for (int j = 0; j < intervalList.size(); j++) {
            final Interval intervalj = intervalList.get(j);
            
            if (intervalj.getStart() < intervali.getEnd() && intervalj.getEnd() > intervali.getStart() && i != j) {
                hashSet.add(new OverlapCoord(Math.max(intervali.getStart(),intervalj.getStart()), 
                                             Math.min(intervali.getEnd(), intervalj.getEnd())));
            }
        }
    }
    
    return hashSet;
}

Find intersections of overlapping intervals

This code finds the intersections of all overlapping intervals. Example: if [0-20], [15-40], and [25-50] are the 3 intervals then the output should be [15-20] and [25-40]. I could not find an answer with complexity less than \$O(n^2)\$. Please let me know a better solution if it exists. Additionally, assume the input intervals are sorted by their start times.

public static Set<OverlapCoord> getOverlap(List<Interval> intervalList) {
    if (intervalList == null) {
        throw new NullPointerException("Input list cannot be null.");
    }
    
    final HashSet<OverlapCoord> hashSet = new HashSet<OverlapCoord>();

    for (int i = 0; i < intervalList.size() - 1; i++) {
        final Interval intervali =  intervalList.get(i);
        
        for (int j = 0; j < intervalList.size(); j++) {
            final Interval intervalj = intervalList.get(j);
            
            if (intervalj.getStart() < intervali.getEnd() && intervalj.getEnd() > intervali.getStart() && i != j) {
                hashSet.add(new OverlapCoord(Math.max(intervali.getStart(),intervalj.getStart()), 
                                             Math.min(intervali.getEnd(), intervalj.getEnd())));
            }
        }
    }
    
    return hashSet;
}
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Source Link
JavaDeveloper
  • 8.5k
  • 29
  • 93
  • 162
Loading