We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7fce08f commit 88784f2Copy full SHA for 88784f2
merge-intervals/gwbaik9717.js
@@ -0,0 +1,33 @@
1
+// Time complexity: O(nlogn)
2
+// Space complexity: O(n)
3
+
4
+/**
5
+ * @param {number[][]} intervals
6
+ * @return {number[][]}
7
+ */
8
+var merge = function (intervals) {
9
+ intervals.sort((a, b) => {
10
+ if (a[0] === b[0]) {
11
+ return a[1] - b[1];
12
+ }
13
14
+ return a[0] - b[0];
15
+ });
16
17
+ const answer = [intervals[0]];
18
19
+ for (let i = 1; i < intervals.length; i++) {
20
+ const current = intervals.at(i);
21
+ const prev = answer.at(-1);
22
23
+ // No overlapping
24
+ if (current[0] > prev[1]) {
25
+ answer.push(current);
26
+ continue;
27
28
29
+ answer[answer.length - 1] = [prev[0], Math.max(prev[1], current[1])];
30
31
32
+ return answer;
33
+};
0 commit comments