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 70ef94c commit c1b5919Copy full SHA for c1b5919
merge-intervals/HC-kang.ts
@@ -0,0 +1,24 @@
1
+/**
2
+ * https://leetcode.com/problems/merge-intervals
3
+ * T.C. O(n logn)
4
+ * S.C. O(n)
5
+ */
6
+function merge(intervals: number[][]): number[][] {
7
+ intervals.sort((a, b) => a[0] - b[0]); // T.C. O(n logn)
8
+
9
+ const result = [intervals[0]]; // S.C. O(n)
10
11
+ // T.C. O(n)
12
+ for (let i = 1; i < intervals.length; i++) {
13
+ const last = result[result.length - 1];
14
+ const current = intervals[i];
15
16
+ if (last[1] >= current[0]) {
17
+ last[1] = Math.max(last[1], current[1]);
18
+ } else {
19
+ result.push(current);
20
+ }
21
22
23
+ return result;
24
+}
0 commit comments