Skip to content

Commit 4310bb1

Browse files
committed
feat: 2251. Number of Flowers in Full Bloom : 差分
1 parent f1ef439 commit 4310bb1

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {number[][]} flowers
3+
* @param {number[]} persons
4+
* @return {number[]}
5+
*/
6+
const fullBloomFlowers = function (f, p) {
7+
const l = []; const l2i = {}
8+
for (const [s, e] of f) {
9+
l.push(s, e + 1)
10+
}
11+
l.sort((a, b) => a - b)
12+
for (let i = 0; i < l.length; i++) l2i[l[i]] = i
13+
const diff = Array(l.length).fill(0)
14+
for (const [s, e] of f) {
15+
diff[l2i[s]] += 1
16+
diff[l2i[e + 1]] -= 1
17+
}
18+
let cur = 0; let i = 0; const ans = []
19+
p.map((x, idx) => [x, idx])
20+
.sort((a, b) => a[0] - b[0])
21+
.forEach(([x, idx]) => {
22+
while (l[i] <= x) cur += diff[i++]
23+
ans[idx] = cur
24+
})
25+
return ans
26+
}

leetcode/残酷刷题/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
- 850. Rectangle Area II, 二维差分
5050
- 2132. Stamping the Grid
5151
- 1109. Corporate Flight Bookings: 差分
52+
- 2251. Number of Flowers in Full Bloom: 差分
5253

5354
### 排序
5455

0 commit comments

Comments
 (0)