Skip to content

Commit 33c4c16

Browse files
committed
https://leetcode.cn/problems/er-cha-shu-ran-se-UGC/
1 parent 674a0ee commit 33c4c16

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

README.md

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

1111
### maven
1212

13+
https://jitpack.io/#masx200/leetcode-test
14+
1315
[![](https://jitpack.io/v/masx200/leetcode-test.svg)](https://jitpack.io/#masx200/leetcode-test)
1416

1517
To get a Git project into your build:
@@ -19,29 +21,32 @@ Step 1. Add the JitPack repository to your build file
1921
maven
2022

2123
```xml
22-
<repositories>
23-
<repository>
24-
<id>jitpack.io</id>
25-
<url>https://jitpack.io</url>
26-
</repository>
27-
</repositories>
24+
<repositories>
25+
<repository>
26+
<id>jitpack.io</id>
27+
<url>https://jitpack.io</url>
28+
</repository>
29+
</repositories>
2830
```
2931

3032
Step 2. Add the dependency
3133

3234
```xml
33-
<dependency>
34-
<groupId>com.github.masx200</groupId>
35-
<artifactId>leetcode-test</artifactId>
36-
<version>10.9.2</version>
37-
</dependency>
35+
<dependency>
36+
<groupId>com.github.masx200</groupId>
37+
<artifactId>leetcode-test</artifactId>
38+
<version>10.9.2</version>
39+
</dependency>
3840
```
3941

4042
##### 包含的内容如下
4143

4244
<details>
45+
4346
<summary>展开查看</summary>
4447

48+
https://leetcode.cn/problems/er-cha-shu-ran-se-UGC/
49+
4550
https://leetcode.cn/problems/combination-sum-iii/
4651

4752
https://leetcode.cn/problems/range-product-queries-of-powers/

er-cha-shu-ran-se-UGC/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
2+
function maxValue(root: TreeNode | null, k: number) {
3+
return Math.max(...dfs(root, k));
4+
}
5+
function dfs(root: TreeNode | null, k: number) {
6+
const ans = Array(k + 1).fill(0);
7+
if (!root) return ans;
8+
9+
const left = dfs(root.left, k);
10+
const right = dfs(root.right, k);
11+
12+
ans.fill(left[k] + right[k]);
13+
14+
for (let i = 1; i <= k; i++) {
15+
let temp = -Infinity;
16+
for (let j = 0; j <= i - 1; j++) {
17+
temp = Math.max(temp, left[j] + right[i - 1 - j]);
18+
}
19+
ans[i] = Math.max(ans[i], temp + root.val);
20+
}
21+
22+
return ans;
23+
}
24+
export default maxValue;

0 commit comments

Comments
 (0)