Skip to content

Commit ed5019a

Browse files
NoyeArkcursoragent
andcommitted
feat: 20260208 check in
Co-authored-by: Cursor <[email protected]>
1 parent 2331986 commit ed5019a

4 files changed

Lines changed: 163 additions & 4 deletions

File tree

leetcode/3-动态规划(基础版)/8-背包问题/518. 零钱兑换 II.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# [518. 零钱兑换 II](https://leetcode.cn/problems/coin-change-ii/description/)
22

3-
> **日期:** 2025-05-12
4-
> **所用时间:** 10min
3+
> **日期:** 2025-05-12、2026-02-08
4+
> **所用时间:** 10min
5+
> **知识点:** 动态规划、完全背包
56
67
## 1. 记忆化搜索
78

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# [110. 平衡二叉树](https://leetcode.cn/problems/balanced-binary-tree/description/)
2+
3+
> **日期**:2026-02-08
4+
> **所用时间**:10min
5+
> **知识点**:二叉树、递归、DFS
6+
7+
## 1. 题目描述
8+
9+
给定一个二叉树,判断它是否是**高度平衡**的。
10+
11+
高度平衡二叉树的定义为:一个二叉树,其中**每个节点的左右子树的高度差不超过 1**
12+
13+
**示例 1:**
14+
```
15+
输入:root = [3,9,20,null,null,15,7]
16+
输出:true
17+
解释:
18+
3
19+
/ \
20+
9 20
21+
/ \
22+
15 7
23+
左右子树高度差不超过 1,是平衡二叉树。
24+
```
25+
26+
**示例 2:**
27+
```
28+
输入:root = [1,2,2,3,3,null,null,4,4]
29+
输出:false
30+
解释:
31+
1
32+
/ \
33+
2 2
34+
/ \
35+
3 3
36+
/ \
37+
4 4
38+
节点 1 的左右子树高度差为 2,不是平衡二叉树。
39+
```
40+
41+
**示例 3:**
42+
```
43+
输入:root = []
44+
输出:true
45+
解释:空树视为平衡二叉树。
46+
```
47+
48+
## 2. 解题算法
49+
50+
### 2.1 算法思路
51+
52+
采用 DFS 自底向上递归:
53+
54+
1. **dfs(root)** 返回以 root 为根的子树高度。
55+
2. **平衡条件**:对每个节点,`|左子树高度 - 右子树高度| ≤ 1`
56+
3. **递归过程**
57+
- 空节点返回高度 0。
58+
- 递归求左右子树高度。
59+
-`|left - right| > 1`,标记为不平衡。
60+
- 返回当前子树高度:`max(left, right) + 1`
61+
62+
### 2.2 复杂度分析
63+
64+
- **时间复杂度**:$O(n)$,其中 $n$ 为节点数,每个节点访问一次。
65+
- **空间复杂度**:$O(h)$,递归栈深度为树高 $h$,最坏 $O(n)$。
66+
67+
### 2.3 代码实现
68+
69+
**Python3**
70+
71+
```python
72+
class Solution:
73+
def isBalanced(self, root: Optional[TreeNode]) -> bool:
74+
ans = True
75+
76+
def dfs(root):
77+
if not root:
78+
return 0
79+
nonlocal ans
80+
left = dfs(root.left)
81+
right = dfs(root.right)
82+
ans &= abs(left - right) <= 1
83+
return max(left, right) + 1
84+
dfs(root)
85+
return ans
86+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# [1653. 使字符串平衡的最少删除次数](https://leetcode.cn/problems/minimum-deletions-to-make-string-balanced/description/)
2+
3+
> **日期**:2026-02-08
4+
> **所用时间**:10min
5+
> **知识点**:动态规划、字符串
6+
7+
## 1. 题目描述
8+
9+
给定一个仅由字符 `'a'``'b'` 组成的字符串 `s`。删除若干字符使得字符串**平衡**
10+
11+
平衡字符串定义为:字符串中不存在一个 `'b'``'a'` 之前。即所有 `'a'` 必须在所有 `'b'` 之前,形如 `a...a b...b`
12+
13+
请返回使字符串平衡的**最少删除次数**
14+
15+
**示例 1:**
16+
```
17+
输入:s = "aababbab"
18+
输出:2
19+
解释:可以删除下标 2 和 6 的 'b',得到 "aaabab";或删除下标 3 和 6 的 'b',得到 "aabbaa"。都是平衡字符串。
20+
```
21+
22+
**示例 2:**
23+
```
24+
输入:s = "bbaaaaabb"
25+
输出:2
26+
解释:删除前两个 'b',得到 "aaaaabb"。
27+
```
28+
29+
## 2. 解题算法
30+
31+
### 2.1 算法思路
32+
33+
等价于求**保留的最长平衡子序列**,答案 = $n - \max(\text{保留长度})$。
34+
35+
平衡子序列形如 `a*a*b*b*`,用 DP 维护:
36+
- `last_a`:以 `'a'` 结尾的最长平衡子序列长度
37+
- `last_b`:以 `'b'` 结尾的最长平衡子序列长度
38+
39+
遍历每个字符:
40+
- 遇到 `'a'`:只能接在 `last_a` 后面(`'a'` 必须在 `'b'` 前)
41+
- 遇到 `'b'`:可接在 `last_a``last_b` 后面
42+
43+
最终答案为 $n - \max(\text{所有状态})$。
44+
45+
### 2.2 复杂度分析
46+
47+
- **时间复杂度**:$O(n)$,单次遍历字符串。
48+
- **空间复杂度**:$O(1)$,仅使用常数个变量。
49+
50+
### 2.3 代码实现
51+
52+
**Python3**
53+
54+
```python
55+
class Solution:
56+
def minimumDeletions(self, s: str) -> int:
57+
# f[i]表示以s[i]结尾满足要求的子串的最大长度
58+
59+
n = len(s)
60+
f = [1] * n
61+
last_a = int(s[0] == 'a')
62+
last_b = int(s[0] == 'b')
63+
for i in range(1, n):
64+
if s[i] == 'a':
65+
f[i] += last_a
66+
last_a = f[i]
67+
else:
68+
f[i] += max(last_a, last_b)
69+
last_b = f[i]
70+
return n - max(f)
71+
```

leetcode/其他/279. 完全平方数.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# [279. 完全平方数](https://leetcode.cn/problems/perfect-squares/description/)
22

3-
> **日期**:2025-05-12
4-
> **所用时间**:3min
3+
> **日期**:2025-05-12
4+
> **所用时间**:3min
5+
> **知识点**:动态规划、完全背包
56
67
## 1. 动态规划
78

0 commit comments

Comments
 (0)