Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

merge intervals

/*
  This implementation demonstrates how to 
  merge overlapping intervals in a non-empty
  array of arbitrary intervals. Each interval
  is an array of two ints, with first element
  being start and second the end of interval.

  Let n be the number of intervals to process.

  Time complexity: O(nlog2(n))
  Space complexity: O(n)
*/

function mergeOverlappingIntervals(intervals) {
  // Sort input intervals by their lower-bound
  const sortedIntervals = intervals.sort((a, b) => a[0] - b[0]);
  // Resulting array of merged intervals
  // It will serve as a stack when solving problem
  const mergedIntervals = [];
  let currentInterval = sortedIntervals[0];
  mergedIntervals.push(currentInterval);

  for (const nextInterval of sortedIntervals) {
    const [_, currentIntervalEnd] = currentInterval;
    const [nextIntervalStart, nextIntervalEnd] = nextInterval;

    if (currentIntervalEnd >= nextIntervalStart) {
      // If current and next intervals overlap, then merge them
      currentInterval[1] = Math.max(currentIntervalEnd, nextIntervalEnd);
    } else {
      currentInterval = nextInterval;
      mergedIntervals.push(currentInterval);
    }
  }

  return mergedIntervals;
}

const intervals = [
  [1, 2],
  [3, 5],
  [4, 7],
  [6, 8],
  [9, 10],
];
// Below prints: [ [ 1, 2 ], [ 3, 8 ], [ 9, 10 ] ]
console.log(mergeOverlappingIntervals(intervals));
Comment

Merge Intervals

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
public class Solution {
    public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) {
 
        ArrayList<Interval> result = new ArrayList<Interval>();
 
        for(Interval interval: intervals){
            if(interval.end < newInterval.start){
                result.add(interval);
            }else if(interval.start > newInterval.end){
                result.add(newInterval);
                newInterval = interval;        
            }else if(interval.end >= newInterval.start || interval.start <= newInterval.end){
                newInterval = new Interval(Math.min(interval.start, newInterval.start), Math.max(newInterval.end, interval.end));
            }
        }
 
        result.add(newInterval); 
 
        return result;
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: stop python script nodejs 
Javascript :: js number padding to number of characters 
Javascript :: javascript ISO Date Formats 
Javascript :: localstorage api 
Javascript :: node js express session expiration 
Javascript :: ping discord by autocode 
Javascript :: express cookieparser 
Javascript :: navigator user media check if camera is availabe 
Javascript :: url_for javascipt 
Javascript :: destructuring in es6 
Javascript :: round number javascript 
Javascript :: jsx example 
Javascript :: clean code javascript 
Javascript :: sort array in javascript 
Javascript :: d3 v6 
Javascript :: how to add two floats 
Javascript :: aos animation 
Javascript :: jsx return greatest number between two numbers 
Javascript :: moment duratuion from hours 
Javascript :: get string length javascript 
Javascript :: check if string Array javascript 
Javascript :: javascript function add two numbers 
Javascript :: react native add bottom tab and drawer menu 
Javascript :: Updating javascript object property 
Javascript :: create table using jade 
Javascript :: round value down html 
Javascript :: what is closure in javascript 
Javascript :: js ctx dash line 
Javascript :: for of loop ecmascript 6 
Javascript :: how to send csrf middleware token in django ajax 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =