deno.land / x / masx200_leetcode_test@10.6.5 / most-stones-removed-with-same-row-or-column / index.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
export default function removeStones(stones: [number, number][]): number { const parent_row = new Map<number, Set<[row: number, col: number]>>(); const parent_col = new Map<number, Set<[row: number, col: number]>>();
function merge( ar: [row: number, col: number], rp: Set<[row: number, col: number]>, ) { parent_row.set(ar[0], rp);
parent_col.set(ar[1], rp); rp.add(ar); }
for (const stone of stones) { const [row, col] = stone;
const rp = parent_row.get(row); const cp = parent_col.get(col);
if ((parent_row.has(row) || parent_col.has(col)) && rp || cp) { if (rp === cp && cp && rp) { merge(stone, rp); } else { combine(stone, rp, cp); } } else { create(stone, row, col); } }
function create(stone: [number, number], row: number, col: number) { const set = new Set([stone]); parent_row.set(row, set);
parent_col.set(col, set); } function combine( stone: [number, number], rp: Set<[row: number, col: number]> | undefined, cp: Set<[row: number, col: number]> | undefined, ) { const series: Set<[row: number, col: number]> = rp || cp || new Set();
cp?.forEach((s) => series.add(s)); rp?.forEach((s) => series.add(s)); merge(stone, series);
series.forEach(([row, col]) => { parent_col.set(col, series); parent_row.set(row, series); }); } return stones.length - new Set([...parent_col.values(), ...parent_row.values()]).size;}
masx200_leetcode_test
leetcode题解和一些测试工具
DocumentationDocumentation
GitHub Repositorymasx200/leetcode-test
GitHub Stars
4

Version Info

Tagged at
4 months ago