Skip to content

Commit fc8308b

Browse files
committed
closeLampInTree
1 parent d59c314 commit fc8308b

File tree

3 files changed

+118
-15
lines changed

3 files changed

+118
-15
lines changed

KnLfVT/index.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
2-
3-
export default function expandBinaryTree(
4-
root: TreeNode | null,
5-
): TreeNode | null {
6-
return root
7-
? new TreeNode(
8-
root.val,
9-
root.left ? new TreeNode(-1, expandBinaryTree(root.left)) : null,
10-
root.right
11-
? new TreeNode(-1, null, expandBinaryTree(root.right))
12-
: null,
13-
)
14-
: null;
15-
}
1+
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
2+
3+
export default function expandBinaryTree(
4+
root: TreeNode | null,
5+
): TreeNode | null {
6+
return root
7+
? new TreeNode(
8+
root.val,
9+
root.left ? new TreeNode(-1, expandBinaryTree(root.left)) : null,
10+
root.right
11+
? new TreeNode(-1, null, expandBinaryTree(root.right))
12+
: null,
13+
)
14+
: null;
15+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Step 2. Add the dependency
4545

4646
<summary>展开查看</summary>
4747

48+
https://leetcode.cn/problems/U7WvvU/
49+
4850
https://leetcode.cn/problems/KnLfVT/
4951

5052
https://leetcode.cn/problems/letter-case-permutation/

U7WvvU/index.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
2+
3+
export default function closeLampInTree(root: TreeNode | null): number {
4+
function dfs(root: TreeNode | null) {
5+
if (!root) return [0, 0, 0, 0]; // 空节点不需要操作
6+
7+
const [lAllOpen, lAllClose, lJustSelf, lJustNotSelf] = dfs(root.left);
8+
const [rAllOpen, rAllClose, rJustSelf, rJustNotSelf] = dfs(root.right);
9+
10+
const open = lAllOpen + rAllOpen;
11+
const close = lAllClose + rAllClose;
12+
const self = lJustSelf + rJustSelf;
13+
const notSelf = lJustNotSelf + rJustNotSelf;
14+
15+
let allOpen = Infinity;
16+
let allClose = Infinity;
17+
let justSelf = Infinity;
18+
let justNotSelf = Infinity;
19+
20+
// 想达到全开
21+
if (root.val) {
22+
// 若当前灯开
23+
// 左右全开
24+
allOpen = Math.min(allOpen, open, close + 2, self + 2, notSelf + 2);
25+
} else {
26+
// 若当前灯关
27+
allOpen = Math.min(
28+
allOpen,
29+
open + 1,
30+
close + 1,
31+
self + 3,
32+
notSelf + 1,
33+
);
34+
}
35+
36+
// 想达到全关
37+
if (root.val) {
38+
// 灯自己开着
39+
allClose = Math.min(
40+
allClose,
41+
open + 1,
42+
close + 1,
43+
self + 1,
44+
notSelf + 3,
45+
);
46+
} else {
47+
allClose = Math.min(
48+
allClose,
49+
open + 2,
50+
close,
51+
self + 2,
52+
notSelf + 2,
53+
);
54+
}
55+
56+
// 达到只有头开着
57+
if (root.val) {
58+
justSelf = Math.min(
59+
justSelf,
60+
open + 2,
61+
close,
62+
self + 2,
63+
notSelf + 2,
64+
);
65+
} else {
66+
// 关着
67+
justSelf = Math.min(
68+
justSelf,
69+
open + 1,
70+
close + 1,
71+
self + 1,
72+
notSelf + 3,
73+
);
74+
}
75+
76+
// 达到只有头关着
77+
if (root.val) {
78+
// 头开着
79+
justNotSelf = Math.min(
80+
justNotSelf,
81+
open + 1,
82+
close + 1,
83+
self + 3,
84+
notSelf + 1,
85+
);
86+
} else {
87+
justNotSelf = Math.min(
88+
justNotSelf,
89+
open,
90+
close + 2,
91+
self + 2,
92+
notSelf + 2,
93+
);
94+
}
95+
96+
return [allOpen, allClose, justSelf, justNotSelf];
97+
}
98+
99+
const [, allClose] = dfs(root);
100+
return allClose;
101+
}

0 commit comments

Comments
 (0)