Skip to content

Commit 8b66332

Browse files
committed
feat: 56. Merge Intervals : 扫描线
1 parent 7c7520b commit 8b66332

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function merge(intervals: number[][]): number[][] {
2+
const a = []
3+
for (const [s, e] of intervals) {
4+
a.push([s, 1], [e, -1])
5+
}
6+
a.sort((x, y) => {
7+
if (x[0] === y[0]) {
8+
return -(x[1] - y[1])
9+
}
10+
return x[0]-y[0]
11+
})
12+
let cnt = 0, ans = [], tmp = []
13+
for (const t of a) {
14+
cnt += t[1]
15+
if (t[1] === 1 && cnt === 1) {
16+
tmp.push(t[0])
17+
} else if (cnt === 0) {
18+
tmp.push(t[0])
19+
ans.push(tmp)
20+
tmp = []
21+
}
22+
}
23+
return ans
24+
};

leetcode/残酷刷题/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
- [字符串哈希](#字符串哈希)
3737
- [括号匹配](#括号匹配)
3838
- [分治 (Divide and Conquer)](#分治-divide-and-conquer)
39+
- [扫描线 (Sweep Line)](#扫描线-sweep-line)
3940
- [Links](#links)
4041

4142
## Problems
@@ -266,6 +267,10 @@
266267
- 315. Count of Smaller Numbers After Self
267268
- 2179. Count Good Triplets in an Array
268269

270+
### 扫描线 (Sweep Line)
271+
272+
- 56. Merge Intervals
273+
269274
## Links
270275

271276
- https://wisdompeak.github.io/lc-score-board/

0 commit comments

Comments
 (0)