Skip to content

[pull] main from doocs:main #495

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

Merged
merged 3 commits into from
Mar 24, 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
135 changes: 134 additions & 1 deletion lcp/LCP 67. 装饰树/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,155 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:递归**

我们设计一个函数 $dfs(root)$,表示将灯饰插入以 $root$ 为根节点的树中,返回插入灯饰后的树的根节点。那么答案就是 $dfs(root)$。

函数 $dfs(root)$ 的逻辑如下:

- 若 $root$ 为空,则返回空;
- 否则,递归地对 $root$ 的左右子树分别调用 $dfs$ 函数,得到插入灯饰后的左右子树的根节点 $l$ 和 $r$;
- 若 $l$ 不为空,则我们创建一个新节点 $TreeNode(-1, l, null)$,并将其作为 $root$ 的左子节点;
- 若 $r$ 不为空,则我们创建一个新节点 $TreeNode(-1, null, r)$,并将其作为 $root$ 的右子节点;

最后,返回 $root$。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是树的节点数。

<!-- tabs:start -->

### **Python3**

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

```python

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def expandBinaryTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
def dfs(root):
if root is None:
return None
l, r = dfs(root.left), dfs(root.right)
if l:
root.left = TreeNode(-1, l)
if r:
root.right = TreeNode(-1, None, r)
return root

return dfs(root)
```

### **Java**

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

```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode expandBinaryTree(TreeNode root) {
return dfs(root);
}

private TreeNode dfs(TreeNode root) {
if (root == null) {
return null;
}
TreeNode l = dfs(root.left);
TreeNode r = dfs(root.right);
if (l != null) {
root.left = new TreeNode(-1, l, null);
}
if (r != null) {
root.right = new TreeNode(-1, null, r);
}
return root;
}
}
```

### **C++**

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* expandBinaryTree(TreeNode* root) {
function<TreeNode*(TreeNode*)> dfs = [&](TreeNode* root) -> TreeNode* {
if (!root) {
return nullptr;
}
TreeNode* l = dfs(root->left);
TreeNode* r = dfs(root->right);
if (l) {
root->left = new TreeNode(-1, l, nullptr);
}
if (r) {
root->right = new TreeNode(-1, nullptr, r);
}
return root;
};
return dfs(root);
}
};
```

### **Go**

```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func expandBinaryTree(root *TreeNode) *TreeNode {
var dfs func(*TreeNode) *TreeNode
dfs = func(root *TreeNode) *TreeNode {
if root == nil {
return root
}
l, r := dfs(root.Left), dfs(root.Right)
if l != nil {
root.Left = &TreeNode{-1, l, nil}
}
if r != nil {
root.Right = &TreeNode{-1, nil, r}
}
return root
}
return dfs(root)
}
```

### **...**
Expand Down
31 changes: 31 additions & 0 deletions lcp/LCP 67. 装饰树/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* expandBinaryTree(TreeNode* root) {
function<TreeNode*(TreeNode*)> dfs = [&](TreeNode* root) -> TreeNode* {
if (!root) {
return nullptr;
}
TreeNode* l = dfs(root->left);
TreeNode* r = dfs(root->right);
if (l) {
root->left = new TreeNode(-1, l, nullptr);
}
if (r) {
root->right = new TreeNode(-1, nullptr, r);
}
return root;
};
return dfs(root);
}
};
25 changes: 25 additions & 0 deletions lcp/LCP 67. 装饰树/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func expandBinaryTree(root *TreeNode) *TreeNode {
var dfs func(*TreeNode) *TreeNode
dfs = func(root *TreeNode) *TreeNode {
if root == nil {
return root
}
l, r := dfs(root.Left), dfs(root.Right)
if l != nil {
root.Left = &TreeNode{-1, l, nil}
}
if r != nil {
root.Right = &TreeNode{-1, nil, r}
}
return root
}
return dfs(root)
}
35 changes: 35 additions & 0 deletions lcp/LCP 67. 装饰树/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode expandBinaryTree(TreeNode root) {
return dfs(root);
}

private TreeNode dfs(TreeNode root) {
if (root == null) {
return null;
}
TreeNode l = dfs(root.left);
TreeNode r = dfs(root.right);
if (l != null) {
root.left = new TreeNode(-1, l, null);
}
if (r != null) {
root.right = new TreeNode(-1, null, r);
}
return root;
}
}
19 changes: 19 additions & 0 deletions lcp/LCP 67. 装饰树/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def expandBinaryTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
def dfs(root):
if root is None:
return None
l, r = dfs(root.left), dfs(root.right)
if l:
root.left = TreeNode(-1, l)
if r:
root.right = TreeNode(-1, None, r)
return root

return dfs(root)
94 changes: 93 additions & 1 deletion lcp/LCP 68. 美观的花束/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,114 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:双指针**

我们用双指针 $j$ 和 $i$ 分别指向当前窗口的左右端点,用数组或哈希表 $d$ 记录当前窗口内的元素以及出现的次数。

遍历数组 $flowers$,每一次我们将 $flowers[i]$ 加入到窗口中,即 $d[flowers[i]]++$,然后判断 $d[flowers[i]]$ 是否大于 $cnt$,如果大于 $cnt$,则我们需要将 $flowers[j]$ 从窗口中移除,即 $d[flowers[j]]--$,并将 $j$ 右移,直到 $d[flowers[i]] \leq cnt$。此时窗口内的元素都不超过 $cnt$ 个,因此我们可以将 $i - j + 1$ 加到答案中。

最后返回答案即可。

时间复杂度 $O(n)$,空间复杂度 $O(m)$。其中 $n$ 和 $m$ 分别为数组 $flowers$ 的长度以及数组 $flowers$ 中的最大值。

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def beautifulBouquet(self, flowers: List[int], cnt: int) -> int:
mod = 10**9 + 7
d = Counter()
ans = j = 0
for i, x in enumerate(flowers):
d[x] += 1
while d[x] > cnt:
d[flowers[j]] -= 1
j += 1
ans = (ans + i - j + 1) % mod
return ans
```

### **Java**

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

```java
class Solution {
public int beautifulBouquet(int[] flowers, int cnt) {
int mx = 0;
for (int x : flowers) {
mx = Math.max(mx, x);
}
int[] d = new int[mx + 1];
long ans = 0;
final int mod = (int) 1e9 + 7;
for (int i = 0, j = 0; i < flowers.length; ++i) {
++d[flowers[i]];
while (d[flowers[i]] > cnt) {
--d[flowers[j++]];
}
ans = (ans + i - j + 1) % mod;
}
return (int) ans;
}
}
```

### **C++**

```cpp
class Solution {
public:
int beautifulBouquet(vector<int>& flowers, int cnt) {
int mx = *max_element(flowers.begin(), flowers.end());
int d[mx + 1];
memset(d, 0, sizeof(d));
long long ans = 0;
const int mod = 1e9 + 7;
for (int i = 0, j = 0; i < flowers.size(); ++i) {
++d[flowers[i]];
while (d[flowers[i]] > cnt) {
--d[flowers[j++]];
}
ans = (ans + i - j + 1) % mod;
}
return ans;
}
};
```

### **Go**

```go
func beautifulBouquet(flowers []int, cnt int) (ans int) {
mx := 0
for _, x := range flowers {
mx = max(mx, x)
}
d := make([]int, mx+1)
j := 0
const mod = 1e9 + 7
for i, x := range flowers {
d[x]++
for d[x] > cnt {
d[flowers[j]]--
j++
}
ans = (ans + i - j + 1) % mod
}
return
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
```

### **...**
Expand Down
Loading