Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lcp problem: No.10 #1755

Merged
merged 1 commit into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 125 additions & 1 deletion lcp/LCP 10. 二叉树任务调度/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,139 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def minimalExecTime(self, root: TreeNode) -> float:
def dfs(root: TreeNode) -> Tuple[int, int]:
if not root:
return 0, 0
s1, t1 = dfs(root.left)
s2, t2 = dfs(root.right)
return s1 + s2 + root.val, max(t1, t2, (s1 + s2) / 2) + root.val

return dfs(root)[1]
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public double minimalExecTime(TreeNode root) {
return dfs(root)[1];
}

private double[] dfs(TreeNode root) {
if (root == null) {
return new double[] {0, 0};
}
double[] left = dfs(root.left);
double[] right = dfs(root.right);
double s = left[0] + right[0] + root.val;
double t = Math.max(Math.max(left[1], right[1]), (left[0] + right[0]) / 2) + root.val;
return new double[] {s, t};
}
}
```

### **C++**

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
double minimalExecTime(TreeNode* root) {
function<pair<double, double>(TreeNode*)> dfs = [&](TreeNode* root) -> pair<double, double> {
if (!root) {
return {0, 0};
}
auto [s1, t1] = dfs(root->left);
auto [s2, t2] = dfs(root->right);
double s = s1 + s2 + root->val;
double t = max({t1, t2, (s1 + s2) / 2}) + root->val;
return {s, t};
};
auto [_, t] = dfs(root);
return t;
}
};
```

### **Go**

```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func minimalExecTime(root *TreeNode) float64 {
var dfs func(*TreeNode) (float64, float64)
dfs = func(root *TreeNode) (float64, float64) {
if root == nil {
return 0, 0
}
s1, t1 := dfs(root.Left)
s2, t2 := dfs(root.Right)
s := s1 + s2 + float64(root.Val)
t := math.Max(math.Max(t1, t2), (s1+s2)/2) + float64(root.Val)
return s, t
}
_, t := dfs(root)
return t
}
```

### **TypeScript**

```ts
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function minimalExecTime(root: TreeNode | null): number {
const dfs = (root: TreeNode | null): [number, number] => {
if (root === null) {
return [0, 0];
}
const [s1, t1] = dfs(root.left);
const [s2, t2] = dfs(root.right);
const s = s1 + s2 + root.val;
const t = Math.max(t1, t2, (s1 + s2) / 2) + root.val;
return [s, t];
};
return dfs(root)[1];
}
```

### **...**
Expand Down
26 changes: 26 additions & 0 deletions lcp/LCP 10. 二叉树任务调度/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
double minimalExecTime(TreeNode* root) {
function<pair<double, double>(TreeNode*)> dfs = [&](TreeNode* root) -> pair<double, double> {
if (!root) {
return {0, 0};
}
auto [s1, t1] = dfs(root->left);
auto [s2, t2] = dfs(root->right);
double s = s1 + s2 + root->val;
double t = max({t1, t2, (s1 + s2) / 2}) + root->val;
return {s, t};
};
auto [_, t] = dfs(root);
return t;
}
};
23 changes: 23 additions & 0 deletions lcp/LCP 10. 二叉树任务调度/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func minimalExecTime(root *TreeNode) float64 {
var dfs func(*TreeNode) (float64, float64)
dfs = func(root *TreeNode) (float64, float64) {
if root == nil {
return 0, 0
}
s1, t1 := dfs(root.Left)
s2, t2 := dfs(root.Right)
s := s1 + s2 + float64(root.Val)
t := math.Max(math.Max(t1, t2), (s1+s2)/2) + float64(root.Val)
return s, t
}
_, t := dfs(root)
return t
}
25 changes: 25 additions & 0 deletions lcp/LCP 10. 二叉树任务调度/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public double minimalExecTime(TreeNode root) {
return dfs(root)[1];
}

private double[] dfs(TreeNode root) {
if (root == null) {
return new double[] {0, 0};
}
double[] left = dfs(root.left);
double[] right = dfs(root.right);
double s = left[0] + right[0] + root.val;
double t = Math.max(Math.max(left[1], right[1]), (left[0] + right[0]) / 2) + root.val;
return new double[] {s, t};
}
}
10 changes: 10 additions & 0 deletions lcp/LCP 10. 二叉树任务调度/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def minimalExecTime(self, root: TreeNode) -> float:
def dfs(root: TreeNode) -> Tuple[int, int]:
if not root:
return 0, 0
s1, t1 = dfs(root.left)
s2, t2 = dfs(root.right)
return s1 + s2 + root.val, max(t1, t2, (s1 + s2) / 2) + root.val

return dfs(root)[1]
27 changes: 27 additions & 0 deletions lcp/LCP 10. 二叉树任务调度/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function minimalExecTime(root: TreeNode | null): number {
const dfs = (root: TreeNode | null): [number, number] => {
if (root === null) {
return [0, 0];
}
const [s1, t1] = dfs(root.left);
const [s2, t2] = dfs(root.right);
const s = s1 + s2 + root.val;
const t = Math.max(t1, t2, (s1 + s2) / 2) + root.val;
return [s, t];
};
return dfs(root)[1];
}