File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed
Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * 56. Merge Intervals
3+ * https://leetcode.com/problems/merge-intervals/
4+ * Difficulty: Medium
5+ *
6+ * Given an array of intervals where intervals[i] = [starti, endi], merge all
7+ * overlapping intervals, and return an array of the non-overlapping intervals
8+ * that cover all the intervals in the input.
9+ */
10+
11+ /**
12+ * @param {number[][] } intervals
13+ * @return {number[][] }
14+ */
15+ var merge = function ( intervals ) {
16+ intervals . sort ( ( [ a ] , [ b ] ) => a - b ) ;
17+
18+ const result = [ intervals . shift ( ) ] ;
19+ intervals . forEach ( ( [ first , second ] ) => {
20+ const [ , last ] = result [ result . length - 1 ] ;
21+ if ( first <= last ) {
22+ result [ result . length - 1 ] [ 1 ] = Math . max ( second , last ) ;
23+ } else {
24+ result . push ( [ first , second ] ) ;
25+ }
26+ } ) ;
27+
28+ return result ;
29+ } ;
Original file line number Diff line number Diff line change 616153|[ Maximum Subarray] ( ./0053-maximum-subarray.js ) |Easy|
626254|[ Spiral Matrix] ( ./0054-spiral-matrix.js ) |Medium|
636355|[ Jump Game] ( ./0055-jump-game.js ) |Medium|
64+ 56|[ Merge Intervals] ( ./0056-merge-intervals.js ) |Medium|
646557|[ Insert Interval] ( ./0057-insert-interval.js ) |Medium|
656658|[ Length of Last Word] ( ./0058-length-of-last-word.js ) |Easy|
666759|[ Spiral Matrix II] ( ./0059-spiral-matrix-ii.js ) |Medium|
You can’t perform that action at this time.
0 commit comments