File tree 2 files changed +30
-0
lines changed
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 61
61
53|[ Maximum Subarray] ( ./0053-maximum-subarray.js ) |Easy|
62
62
54|[ Spiral Matrix] ( ./0054-spiral-matrix.js ) |Medium|
63
63
55|[ Jump Game] ( ./0055-jump-game.js ) |Medium|
64
+ 56|[ Merge Intervals] ( ./0056-merge-intervals.js ) |Medium|
64
65
57|[ Insert Interval] ( ./0057-insert-interval.js ) |Medium|
65
66
58|[ Length of Last Word] ( ./0058-length-of-last-word.js ) |Easy|
66
67
59|[ Spiral Matrix II] ( ./0059-spiral-matrix-ii.js ) |Medium|
You can’t perform that action at this time.
0 commit comments