1+ /**
2+ * Given a collection of intervals, merge all overlapping intervals.
3+ */
4+ 5+ /**
6+ * Definition for an interval.
7+ * function Interval(start, end) {
8+ * this.start = start;
9+ * this.end = end;
10+ * }
11+ */
12+ /**
13+ * @param {Interval[] } intervals
14+ * @return {Interval[] }
15+ */
16+ 17+ /**
18+ * Given a collection of intervals, merge all overlapping intervals.
19+ */
20+ 21+ /**
22+ * Definition for an interval.
23+ * function Interval(start, end) {
24+ * this.start = start;
25+ * this.end = end;
26+ * }
27+ */
28+ 29+ /**
30+ * @param {Interval[] } intervals
31+ * @return {Interval[] }
32+ */
33+ var merge = function ( intervals ) {
34+ if ( intervals . length == 0 ) {
35+ return intervals ;
36+ }
37+ intervals . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
38+ let res = [ ] ;
39+ res . push ( intervals . reduce ( ( acc , cur ) => {
40+ if ( acc [ 1 ] >= cur [ 0 ] ) {
41+ if ( acc [ 1 ] < cur [ 1 ] ) {
42+ acc [ 1 ] = cur [ 1 ] ;
43+ }
44+ return acc ;
45+ } else {
46+ res . push ( acc ) ;
47+ return cur ;
48+ }
49+ } ) ) ;
50+ return res ;
51+ } ;
0 commit comments