Skip to content

Commit 82fea6e

Browse files
committed
feat: problems for the next week
1 parent 324e852 commit 82fea6e

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @param {number} n
3+
* @param {number[][]} edges
4+
* @return {number[]}
5+
*/
6+
const countSubgraphsForEachDiameter = function (n, edges) {
7+
const e = Array(n).fill(0).map(() => [])
8+
for (const [x, y] of edges) e[x - 1].push(y - 1), e[y - 1].push(x - 1)
9+
const bfs = (st, x) => {
10+
const d = Array(n).fill(1e10)
11+
const q = [x]
12+
d[x] = 0
13+
let maxID = x
14+
while (q.length) {
15+
const cur = q.shift()
16+
for (const y of e[cur]) {
17+
if ((st >> y & 1) && d[y] > d[cur] + 1) {
18+
d[y] = d[cur] + 1
19+
q.push(y)
20+
maxID = y
21+
}
22+
}
23+
}
24+
return {
25+
maxID,
26+
d
27+
}
28+
}
29+
const ans = Array(n - 1).fill(0)
30+
for (let i = 1; i < 1 << n; i++) {
31+
let m = 0; let x
32+
for (let j = 0; j < n; j++) {
33+
if (i >> j & 1) m++, x = j
34+
}
35+
const { maxID: s, d } = bfs(i, x)
36+
let reached = 0
37+
for (let i = 0; i < n; i++) if (d[i] < 1e10) reached++
38+
if (reached < m) continue
39+
const { d: d1 } = bfs(i, s)
40+
let max = 0
41+
for (let j = 0; j < n; j++) {
42+
if (i >> j & 1) {
43+
max = Math.max(max, d1[j])
44+
}
45+
}
46+
// console.log(i.toString(2), s, d1)
47+
ans[max - 1]++
48+
}
49+
return ans
50+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} forbidden
3+
* @param {number} a
4+
* @param {number} b
5+
* @param {number} x
6+
* @return {number}
7+
*/
8+
const minimumJumps = function (forbidden, a, b, x) {
9+
const q = [[0, 0, 0]]; const limit = Math.max(...forbidden, x) + b
10+
const s = new Set(forbidden); const v = {}
11+
while (q.length) {
12+
const [p, d, lastDir] = q.shift()
13+
if (v[`${p}_${lastDir}`]) continue
14+
v[`${p}_${lastDir}`] = 1
15+
if (p === x) return d
16+
if ((p <= limit || s.has(p - b)) && !s.has(p + a)) {
17+
q.push([p + a, d + 1, 1])
18+
}
19+
if (p - b >= 0 && lastDir !== -1 && !s.has(p - b)) {
20+
q.push([p - b, d + 1, -1])
21+
}
22+
}
23+
return -1
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number[][]} bombs
3+
* @return {number}
4+
*/
5+
var maximumDetonation = function(bs) {
6+
const n = bs.length
7+
const e = Array(n).fill(0).map(() => [])
8+
for (let i = 0; i < n; i++) {
9+
for (let j = 0; j < n; j++) {
10+
// i 可以引爆 j
11+
if ((bs[i][0] - bs[j][0])**2 + (bs[i][1] - bs[j][1])**2 <= bs[i][2] ** 2) {
12+
e[i].push(j)
13+
}
14+
}
15+
}
16+
let ans = 0
17+
for (let i = 0; i < n; i++) {
18+
const v = Array(n).fill(0)
19+
v[i] = 1
20+
const q = [i]
21+
while (q.length) {
22+
const cur = q.shift()
23+
for (const y of e[cur]) {
24+
if (v[y]) continue
25+
v[y] = 1
26+
q.push(y)
27+
}
28+
}
29+
ans = Math.max(ans, v.reduce((p, c) => p+c, 0))
30+
}
31+
return ans
32+
};

leetcode/残酷刷题/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@
200200
- 1263. Minimum Moves to Move a Box to Their Target Location
201201
- Deque, 只有 0 和 1 两个边权
202202
- 2258. Escape the Spreading Fire
203+
- 2101. Detonate the Maximum Bombs
204+
- 1654. Minimum Jumps to Reach Home
205+
- 1617. Count Subtrees With Max Distance Between Cities
203206

204207
#### 拓扑排序
205208

0 commit comments

Comments
 (0)