Skip to content

File tree

4 files changed

+108
-5
lines changed

4 files changed

+108
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ leetcode 测试
1010

1111
##### 包含的内容如下
1212

13+
https://leetcode.cn/problems/largest-component-size-by-common-factor
14+
15+
https://leetcode.cn/problems/maximum-level-sum-of-a-binary-tree/
16+
1317
https://leetcode.cn/problems/valid-square/
1418

1519
https://leetcode.cn/problems/wildcard-matching
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
function largestComponentSize(nums: number[]) {
2+
// const max = Math.max(...nums);
3+
const uf = new UnionFind(/*max + 1*/);
4+
for (let n of nums) {
5+
const temp = n;
6+
for (let i = 2; i <= n / i; i++) {
7+
let flag = false;
8+
while (n % i === 0) {
9+
n /= i;
10+
flag = true;
11+
}
12+
if (flag) {
13+
uf.union(temp, i);
14+
}
15+
}
16+
if (n > 1) {
17+
uf.union(n, temp);
18+
}
19+
}
20+
21+
const count: Map<number, number> = new Map();
22+
let res = 0;
23+
for (const n of nums) {
24+
const p = uf.find(n);
25+
// if (!count[p]) count[p] = 0;
26+
// count[p]++;
27+
28+
count.set(p, (count.get(p) ?? 0) + 1);
29+
res = Math.max(res, count.get(p) ?? 0);
30+
}
31+
32+
return res;
33+
}
34+
35+
class UnionFind {
36+
sizes: Map<number, number> = new Map();
37+
parents: Map<number, number> = new Map();
38+
constructor(/*size:number*/) {
39+
// 初始化数组,其值为其索引值
40+
/* this.parents = Array(size)
41+
.fill(0)
42+
.map((_, i) => i);
43+
this.sizes = Array(size).fill(1);
44+
*/
45+
}
46+
47+
/**
48+
* 返回某个节点x的根节点
49+
*/
50+
find(x: number) {
51+
if (x !== (this.parents.get(x) ?? x)) {
52+
this.parents.set(x, this.find(this.parents.get(x) ?? x));
53+
}
54+
return this.parents.get(x) ?? x;
55+
}
56+
57+
/**
58+
* 连接
59+
*/
60+
union(a: number, b: number) {
61+
const fa = this.find(a);
62+
const fb = this.find(b);
63+
if (fa == fb) {
64+
return;
65+
}
66+
const sa = this.sizes.get(fa) ?? 1;
67+
const sb = this.sizes.get(fb) ?? 1;
68+
69+
if (sa < sb) {
70+
this.parents.set(fa, fb);
71+
// fb是root
72+
this.sizes.set(fb, sb + sa);
73+
} else {
74+
this.parents.set(fb, fa);
75+
// fa是root
76+
this.sizes.set(fa, sb + sa);
77+
}
78+
}
79+
}
80+
export default largestComponentSize;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
2+
import { level } from "../binary-tree-level-order-traversal-ii/level.ts";
3+
4+
function maxLevelSum(root: TreeNode | null): number {
5+
if (!root) return 0;
6+
const current: TreeNode[] = [root];
7+
const result: number[] = [];
8+
9+
level(current, (r) => result.push(r.reduce((a, b) => a + b)));
10+
11+
const sums = result;
12+
13+
const max_entries = Array.from(sums.entries()).reduceRight((a, v) =>
14+
a[1] > v[1] ? a : v
15+
);
16+
return max_entries[0] + 1;
17+
}
18+
19+
export default maxLevelSum;

wildcard-matching/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ function check_fixs(s: string, words: string[]): boolean {
4242
if (suffix) {
4343
const matched = str.match(
4444
//@ts-ignore
45-
new RegExp(`^(.*?)${suffix.replaceAll("?", ".")}$`)
45+
new RegExp(`^(.*?)${suffix.replaceAll("?", ".")}$`),
4646
);
4747
if (!matched) return false;
4848
str = matched[1];
4949
}
5050
if (prefix) {
5151
const matched = str.match(
5252
//@ts-ignore
53-
new RegExp(`^${prefix.replaceAll("?", ".")}(.*?)$`)
53+
new RegExp(`^${prefix.replaceAll("?", ".")}(.*?)$`),
5454
);
5555
if (!matched) return false;
5656
str = matched[1];
@@ -65,21 +65,21 @@ function check_regular(s: string, p: string): boolean {
6565
return new RegExp(
6666
//@ts-ignore
6767
"^" + p.replaceAll("?", ".").replaceAll(/\*+/g, ".*") + "$",
68-
"g"
68+
"g",
6969
).test(s);
7070
}
7171
function check_words(s: string, words: string[]): boolean {
7272
// console.log("check_words", s, words);
7373
if (words.length === 0) return true;
7474
const mid_index = words.reduce(
7575
(a, v, i) => (v.length > words[a].length ? i : a),
76-
Math.floor(words.length / 2)
76+
Math.floor(words.length / 2),
7777
);
7878
// const mid_index = Math.floor(words.length / 2);
7979
const middle = words[mid_index];
8080
const matched_array = Array.from(
8181
//@ts-ignore
82-
s.matchAll(new RegExp(`${middle.replaceAll("?", ".")}`, "g"))
82+
s.matchAll(new RegExp(`${middle.replaceAll("?", ".")}`, "g")),
8383
);
8484

8585
// console.log(matched_array);

0 commit comments

Comments
 (0)