forked from itcharge/AlgoNote
-
Notifications
You must be signed in to change notification settings - Fork 0
[pull] main from itcharge:main #173
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
Solutions/1110. 删点成林.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# [1110. 删点成林](https://leetcode.cn/problems/delete-nodes-and-return-forest/) | ||
|
||
- 标签:树、深度优先搜索、数组、哈希表、二叉树 | ||
- 难度:中等 | ||
|
||
## 题目链接 | ||
|
||
- [1110. 删点成林 - 力扣](https://leetcode.cn/problems/delete-nodes-and-return-forest/) | ||
|
||
## 题目大意 | ||
|
||
**描述**:给定二叉树的根节点 $root,ドル树上每个节点都有一个不同的值。 | ||
|
||
如果节点值在 $to\underline{}delete$ 中出现,我们就把该节点从树上删去,最后得到一个森林(一些不相交的树构成的集合)。 | ||
|
||
**要求**:返回森林中的每棵树。你可以按任意顺序组织答案。 | ||
|
||
**说明**: | ||
|
||
- 树中的节点数最大为 1000ドル$。 | ||
- 每个节点都有一个介于 1ドル$ 到 1000ドル$ 之间的值,且各不相同。 | ||
- $to\underline{}delete.length \le 1000$。 | ||
- $to\underline{}delete$ 包含一些从 1ドル$ 到 1000ドル$、各不相同的值。 | ||
|
||
**示例**: | ||
|
||
- 示例 1: | ||
|
||
 | ||
|
||
```python | ||
输入:root = [1,2,3,4,5,6,7], to_delete = [3,5] | ||
输出:[[1,2,null,4],[6],[7]] | ||
``` | ||
|
||
- 示例 2: | ||
|
||
```python | ||
输入:root = [1,2,4,null,3], to_delete = [3] | ||
输出:[[1,2,4]] | ||
``` | ||
|
||
## 解题思路 | ||
|
||
### 思路 1:深度优先搜索 | ||
|
||
将待删除节点数组 $to\underline{}delete$ 转为集合 $deletes,ドル则每次能以 $O(1)$ 的时间复杂度判断节点值是否在待删除节点数组中。 | ||
|
||
如果当前节点值在待删除节点数组中,则删除当前节点后,需要将其左右子树作为一棵树的节点加入到答案数组中。 | ||
|
||
而如果当前节点值不在待删除节点数组中, | ||
|
||
### 思路 1:代码 | ||
|
||
```Python | ||
class Solution: | ||
def delNodes(self, root: Optional[TreeNode], to_delete: List[int]) -> List[TreeNode]: | ||
forest = [] | ||
deletes = set(to_delete) | ||
def dfs(root): | ||
if not root: | ||
return None | ||
root.left = dfs(root.left) | ||
root.right = dfs(root.right) | ||
|
||
if root.val in deletes: | ||
if root.left: | ||
forest.append(root.left) | ||
if root.right: | ||
forest.append(root.right) | ||
return None | ||
else: | ||
return root | ||
|
||
|
||
if dfs(root): | ||
forest.append(root) | ||
return forest | ||
``` | ||
|
||
### 思路 1:复杂度分析 | ||
|
||
- **时间复杂度**:$O(n),ドル其中 $n$ 为二叉树中节点个数。 | ||
- **空间复杂度**:$O(n)$。 | ||
|
67 changes: 67 additions & 0 deletions
Solutions/1641. 统计字典序元音字符串的数目.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# [1641. 统计字典序元音字符串的数目](https://leetcode.cn/problems/count-sorted-vowel-strings/) | ||
|
||
- 标签:数学、动态规划、组合数学 | ||
- 难度:中等 | ||
|
||
## 题目链接 | ||
|
||
- [1641. 统计字典序元音字符串的数目 - 力扣](https://leetcode.cn/problems/count-sorted-vowel-strings/) | ||
|
||
## 题目大意 | ||
|
||
**描述**:给定一个整数 $n$。 | ||
|
||
**要求**:返回长度为 $n$、仅由原音($a$、$e$、$i$、$o$、$u$)组成且按字典序排序的字符串数量。 | ||
|
||
**说明**: | ||
|
||
- 字符串 $a$ 按字典序排列需要满足:对于所有有效的 $i,ドル$s[i]$ 在字母表中的位置总是与 $s[i + 1]$ 相同或在 $s[i+1] $之前。 | ||
- 1ドル \le n \le 50$。 | ||
|
||
**示例**: | ||
|
||
- 示例 1: | ||
|
||
```python | ||
输入:n = 1 | ||
输出:5 | ||
解释:仅由元音组成的 5 个字典序字符串为 ["a","e","i","o","u"] | ||
``` | ||
|
||
- 示例 2: | ||
|
||
```python | ||
输入:n = 2 | ||
输出:15 | ||
解释:仅由元音组成的 15 个字典序字符串为 | ||
["aa","ae","ai","ao","au","ee","ei","eo","eu","ii","io","iu","oo","ou","uu"] | ||
注意,"ea" 不是符合题意的字符串,因为 'e' 在字母表中的位置比 'a' 靠后 | ||
``` | ||
|
||
## 解题思路 | ||
|
||
### 思路 1:组和数学 | ||
|
||
题目要求按照字典序排列,则如果确定了每个元音的出现次数可以确定一个序列。 | ||
|
||
对于长度为 $n$ 的序列,$a$、$e$、$i$、$o$、$u$ 出现次数加起来为 $n$ 次,且顺序为 $a...a \rightarrow e...e \rightarrow i...i \rightarrow o...o \rightarrow u...u$。 | ||
|
||
我们可以看作是将 $n$ 分隔成了 5ドル$ 份,每一份对应一个原音字母的数量。 | ||
|
||
我们可以使用「隔板法」的方式,看作有 $n$ 个球,4ドル$ 个板子,将 $n$ 个球分隔成 5ドル$ 份。 | ||
|
||
则一共有 $n + 4$ 个位置可以放板子,总共需要放 4ドル$ 个板子,则答案为 $C_{n + 4}^4,ドル其中 $C$ 为组和数。 | ||
|
||
### 思路 1:代码 | ||
|
||
```Python | ||
class Solution: | ||
def countVowelStrings(self, n: int) -> int: | ||
return comb(n + 4, 4) | ||
``` | ||
|
||
### 思路 1:复杂度分析 | ||
|
||
- **时间复杂度**:$O(| \sum |),ドル其中 $\sum$ 为字符集,本题中 $| \sum | = 5$ 。 | ||
- **空间复杂度**:$O(1)$。 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.