Skip to content
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
1 change: 0 additions & 1 deletion Index/二分.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,3 @@
| [剑指 Offer 53 - II. 0~n-1中缺失的数字](https://leetcode.cn/problems/que-shi-de-shu-zi-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/que-shi-de-shu-zi-lcof/solution/by-ac_oier-gs6q/) | 简单 | 🤩🤩🤩🤩🤩 |
| [剑指 Offer II 008. 和大于等于 target 的最短子数组](https://leetcode.cn/problems/2VG8Kg/) | [LeetCode 题解链接](https://leetcode.cn/problems/2VG8Kg/solution/by-ac_oier-vw5r/) | 中等 | 🤩🤩🤩🤩 |
| [剑指 Offer II 069. 山峰数组的顶部](https://leetcode-cn.com/problems/B1IidL/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/B1IidL/solution/gong-shui-san-xie-er-fen-san-fen-ji-zhi-lc8zl/) | 简单 | 🤩🤩🤩🤩🤩 |

2 changes: 1 addition & 1 deletion Index/二进制枚举.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
| [784. 字母大小写全排列](https://leetcode.cn/problems/letter-case-permutation/) | [LeetCode 题解链接](https://leetcode.cn/problems/letter-case-permutation/solution/by-ac_oier-x7f0/) | 中等 | 🤩🤩🤩🤩 |
| [805. 数组的均值分割](https://leetcode.cn/problems/split-array-with-same-average/) | [LeetCode 题解链接](https://leetcode.cn/problems/split-array-with-same-average/solution/gong-shui-san-xie-by-ac_oier-flsd/) | 困难 | 🤩🤩🤩🤩 |
| [1239. 串联字符串的最大长度](https://leetcode-cn.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/solution/gong-shui-san-xie-yi-ti-san-jie-jian-zhi-nfeb/) | 中等 | 🤩🤩🤩 |
| [1601. 最多可达成的换楼请求数目](https://leetcode-cn.com/problems/maximum-number-of-achievable-transfer-requests/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-number-of-achievable-transfer-requests/solution/gong-shui-san-xie-er-jin-zhi-mei-ju-by-a-enef/) | 中等 | 🤩🤩🤩🤩 |
| [1601. 最多可达成的换楼请求数目](https://leetcode-cn.com/problems/maximum-number-of-achievable-transfer-requests/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-number-of-achievable-transfer-requests/solution/gong-shui-san-xie-er-jin-zhi-mei-ju-by-a-enef/) | 困难 | 🤩🤩🤩🤩 |
| [2044. 统计按位或能得到最大值的子集数目](https://leetcode-cn.com/problems/count-number-of-maximum-bitwise-or-subsets/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-number-of-maximum-bitwise-or-subsets/solution/by-ac_oier-dos6/) | 困难 | 🤩🤩🤩🤩 |

1 change: 1 addition & 0 deletions Index/计算几何.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
| [587. 安装栅栏](https://leetcode-cn.com/problems/erect-the-fence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/erect-the-fence/solution/by-ac_oier-4xuu/) | 困难 | 🤩🤩🤩🤩🤩 |
| [593. 有效的正方形](https://leetcode.cn/problems/valid-square/) | [LeetCode 题解链接](https://leetcode.cn/problems/valid-square/solution/by-ac_oier-lwdf/) | 中等 | 🤩🤩🤩🤩🤩 |
| [1037. 有效的回旋镖](https://leetcode.cn/problems/valid-boomerang/) | [LeetCode 题解链接](https://leetcode.cn/problems/valid-boomerang/solution/by-ac_oier-eory/) | 简单 | 🤩🤩🤩🤩🤩 |
| [1610. 可见点的最大数目](https://leetcode.cn/problems/maximum-number-of-visible-points/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-number-of-visible-points/solution/gong-shui-san-xie-qiu-ji-jiao-ji-he-ti-b-0bid/) | 困难 | 🤩🤩🤩🤩 |

1 change: 1 addition & 0 deletions LeetCode/1-10/5. 最长回文子串(中等).md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class Solution {
* 空间复杂度:$O(1)$

---

### 总结

今天这道题目,三叶除了提供常规的、时间复杂度为 $O(n^2)$ 的朴素解法以外,还给你提供了关于「回文串」的最优解 Manacher 算法模板,建议有余力的同学可以背过。
Expand Down
10 changes: 5 additions & 5 deletions LeetCode/131-140/131. 分割回文串(中等).md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ Tag : 「回文串」、「回溯算法」、「动态规划」



给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。

回文串 是正着读和反着读都一样的字符串。
给你一个字符串 `s`,请你将 `s` 分割成一些子串,使每个子串都是 回文串 。

返回 `s` 所有可能的分割方案。

回文串 是正着读和反着读都一样的字符串。

示例 1:
```
Expand All @@ -24,8 +24,8 @@ Tag : 「回文串」、「回溯算法」、「动态规划」
```

提示:
* 1 <= s.length <= 16
* s 仅由小写英文字母组成
* $1 <= s.length <= 16$
* `s` 仅由小写英文字母组成

---

Expand Down
6 changes: 3 additions & 3 deletions LeetCode/131-140/132. 分割回文串 II(困难).md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Tag : 「回文串」、「线性 DP」



给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是回文。
给你一个字符串 `s`,请你将 `s` 分割成一些子串,使每个子串都是回文。

返回符合要求的 最少分割次数 。

Expand All @@ -28,8 +28,8 @@ Tag : 「回文串」、「线性 DP」
```

提示:
* 1 <= s.length <= 2000
* s 仅由小写英文字母组成
* $1 <= s.length <= 2000$
* `s` 仅由小写英文字母组成

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Tag : 「二进制枚举」



我们有 n 栋楼,编号从 0 到 n - 1 。每栋楼有若干员工。由于现在是换楼的季节,部分员工想要换一栋楼居住。
我们有 `n` 栋楼,编号从 `0` 到 `n - 1` 。每栋楼有若干员工。由于现在是换楼的季节,部分员工想要换一栋楼居住。

给你一个数组 $requests$ ,其中 $requests[i] = [from_i, to_i]$ ,表示一个员工请求从编号为 $from_i$ 的楼搬到编号为 $to_i$ 的楼。

Expand Down Expand Up @@ -68,7 +68,7 @@ Tag : 「二进制枚举」

我们枚举所有的 $state$ 并进行合法性检查,从中选择出包含请求数的最多(二进制表示中包含 $1$ 个数最多)的合法 $state$,其包含的请求数量即是答案。

其中统计 $state$ 中 $1$ 的个数可以使用 `lowbit`,复杂度为 $O(m)$,判断合法性则直接模拟即可(统计每座建筑的进出数量,最后判定进出数不相等的建筑数量是为 $0$),复杂度为 $O(m)$,整体计算量为不超过 $2*10^6$,可以过。
其中统计 $state$ 中 $1$ 的个数可以使用 `lowbit`,复杂度为 $O(m)$,判断合法性则直接模拟即可(统计每座建筑的进出数量,最后判定进出数不相等的建筑数量是为 $0$),复杂度为 $O(m)$,整体计算量为不超过 $2 \times 10^6$,可以过。

代码:
```Java
Expand Down Expand Up @@ -103,7 +103,7 @@ class Solution {
}
}
```
* 时间复杂度:令 $m$ 为 $requests$ 长度,共有 $2^m$ 种选择状态,计算每个状态的所包含的问题数量复杂度为 $O(m)$,计算某个状态是否合法复杂度为 $O(m)$;整体复杂度为 $O(2^m * m)$
* 时间复杂度:令 $m$ 为 $requests$ 长度,共有 $2^m$ 种选择状态,计算每个状态的所包含的问题数量复杂度为 $O(m)$,计算某个状态是否合法复杂度为 $O(m)$;整体复杂度为 $O(2^m \times m)$
* 空间复杂度:$O(n)$

---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
### 题目描述

这是 LeetCode 上的 **[1769. 移动所有球到每个盒子所需的最小操作数]()** ,难度为 **中等**。

Tag : 「模拟」



有 `n` 个盒子。给你一个长度为 `n` 的二进制字符串 `boxes`,其中 `boxes[i]` 的值为 `'0'` 表示第 `i` 个盒子是 空 的,而 `boxes[i]` 的值为 `'1'` 表示盒子里有 一个 小球。

在一步操作中,你可以将 一个 小球从某个盒子移动到一个与之相邻的盒子中。第 `i` 个盒子和第 `j` 个盒子相邻需满足 `abs(i - j) == 1`。注意,操作执行后,某些盒子中可能会存在不止一个小球。

返回一个长度为 `n` 的数组 `answer`,其中 `answer[i]` 是将所有小球移动到第 `i` 个盒子所需的 最小 操作数。

每个 `answer[i]` 都需要根据盒子的 初始状态 进行计算。

示例 1:
```
输入:boxes = "110"

输出:[1,1,3]

解释:每个盒子对应的最小操作数如下:
1) 第 1 个盒子:将一个小球从第 2 个盒子移动到第 1 个盒子,需要 1 步操作。
2) 第 2 个盒子:将一个小球从第 1 个盒子移动到第 2 个盒子,需要 1 步操作。
3) 第 3 个盒子:将一个小球从第 1 个盒子移动到第 3 个盒子,需要 2 步操作。将一个小球从第 2 个盒子移动到第 3 个盒子,需要 1 步操作。共计 3 步操作。
```
示例 2:
```
输入:boxes = "001011"

输出:[11,8,5,4,3,4]
```

提示:
* $n = boxes.length$
* $1 <= n <= 2000$
* `boxes[i]` 为 `'0'` 或 `'1'`

---

### 模拟

预处理两个与 `boxes` 等长的数组 `l` 和 `r`:$l[i]$ 和 $r[i]$ 分别代表「将 $[0, i]$ 的小球移动到位置 $i$」以及「将 $[i, n - 1]$ 的小球移动到位置 $i$」所需要的步数。

所求的答案数组 `ans` 与数组 `l` 和 `r` 的关系为:$ans[i] = l[i] + r[i]$。

预处理两数组是简单的:分别从两个方向遍历 `boxes`,使用变量 `cur` 代表当前处理到的前缀/后缀的小球总个数,变量 `step` 代表将当前所有前缀/后缀小球移动到位置 $i$ 所需要的步数。

Java 代码:
```Java
class Solution {
public int[] minOperations(String boxes) {
int n = boxes.length();
int[] l = new int[n + 10], r = new int[n + 10];
for (int i = 0, cur = 0, step = 0; i < n; i++) {
step += cur; cur += boxes.charAt(i) - '0'; l[i] = step;
}
for (int i = n - 1, cur = 0, step = 0; i >= 0; i--) {
step += cur; cur += boxes.charAt(i) - '0'; r[i] = step;
}
int[] ans = new int[n];
for (int i = 0; i < n; i++) ans[i] = l[i] + r[i];
return ans;
}
}
```
TypeScript 代码:
```TypeScript
function minOperations(boxes: string): number[] {
const n = boxes.length
const l = new Array<number>(n + 10).fill(0), r = new Array<number>(n + 10).fill(0)
for (let i = 0, cur = 0, step = 0; i < n; i++) {
step += cur; cur += boxes[i] == '1' ? 1 : 0; l[i] = step;
}
for (let i = n - 1, cur = 0, step = 0; i >= 0; i--) {
step += cur; cur += boxes[i] == '1' ? 1 : 0; r[i] = step;
}
const ans = new Array<number>(n).fill(0)
for (let i = 0; i < n; i++) ans[i] = l[i] + r[i]
return ans
}
```
Python 代码:
```Python
class Solution:
def minOperations(self, boxes: str) -> List[int]:
n = len(boxes)
l, r = [0] * n, [0] * n
step, cur = 0, 0
for i in range(n):
step, cur = step + cur, cur + 1 if boxes[i] == '1' else cur
l[i] = step
step, cur = 0, 0
for i in range(n - 1, -1, -1):
step, cur = step + cur, cur + 1 if boxes[i] == '1' else cur
r[i] = step
return [l[i] + r[i] for i in range(n)]
```
* 时间复杂度:$O(n)$
* 空间复杂度:$O(n)$

---

### 最后

这是我们「刷穿 LeetCode」系列文章的第 `No.1769` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。

2 changes: 1 addition & 1 deletion LeetCode/521-530/526. 优美的排列(中等).md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Tag : 「位运算」、「状压 DP」、「动态规划」
```

说明:
* N 是一个正整数,并且不会超过15
* $N$ 是一个正整数,并且不会超过 $15$

---

Expand Down
12 changes: 7 additions & 5 deletions LeetCode/641-650/650. 只有两个键的键盘(中等).md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

Tag : 「动态规划」、「线性 DP」、「数学」、「打表」

最初记事本上只有一个字符 'A' 。你每次可以对这个记事本进行两种操作:

* Copy All(复制全部):复制这个记事本中的所有字符(不允许仅复制部分字符)。
* Paste(粘贴):粘贴 上一次 复制的字符。

给你一个数字 n ,你需要使用最少的操作次数,在记事本上输出 恰好 n 个 'A' 。返回能够打印出 n 个 'A' 的最少操作次数。
最初记事本上只有一个字符 `'A'` 。你每次可以对这个记事本进行两种操作:

* `Copy All(复制全部)`:复制这个记事本中的所有字符(不允许仅复制部分字符)。
* `Paste(粘贴)`:粘贴 上一次 复制的字符。

给你一个数字 `n` ,你需要使用最少的操作次数,在记事本上输出 恰好 `n` 个 `'A'` 。返回能够打印出 `n` 个 `'A'` 的最少操作次数。


示例 1:
Expand All @@ -32,7 +34,7 @@ Tag : 「动态规划」、「线性 DP」、「数学」、「打表」
```

提示:
* 1 <= n <= 1000
* $1 <= n <= 1000$

---

Expand Down
2 changes: 2 additions & 0 deletions LeetCode/671-680/678. 有效的括号字符串(中等).md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

Tag : 「有效括号问题」、「动态规划」、「模拟」



给定一个只包含三种字符的字符串:`(` ,`)` 和 `*`,写一个函数来检验这个字符串是否为有效字符串。

有效字符串具有如下规则:
Expand Down