Skip to content

Commit 3a9a73e

Browse files
committed
feat: leetcode contest 290
1 parent 986a2db commit 3a9a73e

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[][]} nums
3+
* @return {number[]}
4+
*/
5+
var intersection = function(nums) {
6+
const cnt = {}
7+
for (const p of nums) {
8+
for (const a of p) cnt[a] = (cnt[a]||0)+1
9+
}
10+
let ans = []
11+
for (const [k, v] of Object.entries(cnt)) {
12+
if (v === nums.length) ans.push(Number(k))
13+
}
14+
return ans.sort((a, b) => a - b)
15+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[][]} circles
3+
* @return {number}
4+
*/
5+
const countLatticePoints = function (circles) {
6+
let ans = 0
7+
for (let i = 0; i <= 200; i++) {
8+
for (let j = 0; j <= 200; j++) {
9+
let ok = false
10+
for (const [x, y, r] of circles) {
11+
const d = (x - i) * (x - i) + (y - j) * (y - j)
12+
if (d <= r * r) {
13+
ok = true
14+
break
15+
}
16+
}
17+
ans += ok
18+
}
19+
}
20+
return ans
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[][]} rectangles
3+
* @param {number[][]} points
4+
* @return {number[]}
5+
*/
6+
const countRectangles = function (rectangles, points) {
7+
const bh = Array(101).fill(0).map(() => [])
8+
for (const r of rectangles) {
9+
const [l, h] = r
10+
bh[h].push(l)
11+
}
12+
bh.forEach(h => h.sort((a, b) => a - b))
13+
// console.log(bh)
14+
const ans = []
15+
for (const [l, h] of points) {
16+
let cnt = 0
17+
for (let i = h; i <= 100; i++) {
18+
let lft = 0; let rgt = bh[i].length
19+
while (lft < rgt) {
20+
const mid = lft + rgt >> 1
21+
if ((mid === bh[i].length) || bh[i][mid] >= l) rgt = mid
22+
else lft = mid + 1
23+
}
24+
cnt += bh[i].length - lft
25+
// console.log(l, h, i, bh[i], lft)
26+
}
27+
ans.push(cnt)
28+
}
29+
return ans
30+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @param {number[][]} flowers
3+
* @param {number[]} persons
4+
* @return {number[]}
5+
*/
6+
const fullBloomFlowers = function (flowers, persons) {
7+
// 离散化,注意右端点的值是 e+1
8+
const p = []
9+
for (const [s, e] of flowers) p.push(s, e + 1)
10+
p.sort((a, b) => a - b)
11+
const mp = {}
12+
for (let i = 0; i < p.length; i++) mp[p[i]] = i
13+
14+
// 计算差分数组
15+
const diff = Array(p.length).fill(0)
16+
for (const [s, e] of flowers) {
17+
const si = mp[s]; const ei = mp[e + 1]
18+
diff[si] += 1
19+
diff[ei] -= 1
20+
}
21+
22+
// 利用差分数组计算原始数组,表示每个时间点的花的数量
23+
const a = []; let prev = 0
24+
const pi = persons.map((p, i) => [p, i]).sort((a, b) => a[0] - b[0])
25+
let j = 0; const ans = []
26+
for (let i = 0; i < diff.length; i++) {
27+
while (j < pi.length && pi[j][0] < p[i]) {
28+
ans[pi[j][1]] = prev, j++
29+
}
30+
a[i] = prev + diff[i]
31+
prev = a[i]
32+
}
33+
while (j < pi.length) ans[pi[j][1]] = prev, j++
34+
return ans
35+
}
36+
/*
37+
[[19,37],[19,38],[19,35]]
38+
[6,7,21,1,13,37,5,37,46,43]
39+
40+
[0,0,3,0,0,2,0,2,0,0]
41+
42+
[[11,11],[24,46],[3,25],[44,46]]
43+
[1,8,26,7,43,26,1]
44+
45+
[0,1,1,1,1,1,0]
46+
*/

0 commit comments

Comments
 (0)