Skip to content

Commit 169bc75

Browse files
Merge pull request SharingSource#758 from SharingSource/ac_oier
✨feat: add 1769 & 678 & 1601 & 131 & 650 & 526
2 parents 196b445 + 1c742ca commit 169bc75

11 files changed

+138
-19
lines changed

Index/二分.md

-1
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,3 @@
7777
| [剑指 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/) | 简单 | 🤩🤩🤩🤩🤩 |
7878
| [剑指 Offer II 008. 和大于等于 target 的最短子数组](https://leetcode.cn/problems/2VG8Kg/) | [LeetCode 题解链接](https://leetcode.cn/problems/2VG8Kg/solution/by-ac_oier-vw5r/) | 中等 | 🤩🤩🤩🤩 |
7979
| [剑指 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/) | 简单 | 🤩🤩🤩🤩🤩 |
80-

Index/二进制枚举.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
| [784. 字母大小写全排列](https://leetcode.cn/problems/letter-case-permutation/) | [LeetCode 题解链接](https://leetcode.cn/problems/letter-case-permutation/solution/by-ac_oier-x7f0/) | 中等 | 🤩🤩🤩🤩 |
55
| [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/) | 困难 | 🤩🤩🤩🤩 |
66
| [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/) | 中等 | 🤩🤩🤩 |
7-
| [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/) | 中等 | 🤩🤩🤩🤩 |
7+
| [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/) | 困难 | 🤩🤩🤩🤩 |
88
| [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/) | 困难 | 🤩🤩🤩🤩 |
99

Index/计算几何.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
| [587. 安装栅栏](https://leetcode-cn.com/problems/erect-the-fence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/erect-the-fence/solution/by-ac_oier-4xuu/) | 困难 | 🤩🤩🤩🤩🤩 |
44
| [593. 有效的正方形](https://leetcode.cn/problems/valid-square/) | [LeetCode 题解链接](https://leetcode.cn/problems/valid-square/solution/by-ac_oier-lwdf/) | 中等 | 🤩🤩🤩🤩🤩 |
55
| [1037. 有效的回旋镖](https://leetcode.cn/problems/valid-boomerang/) | [LeetCode 题解链接](https://leetcode.cn/problems/valid-boomerang/solution/by-ac_oier-eory/) | 简单 | 🤩🤩🤩🤩🤩 |
6+
| [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/) | 困难 | 🤩🤩🤩🤩 |
67

LeetCode/1-10/5. 最长回文子串(中等).md

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ class Solution {
156156
* 空间复杂度:$O(1)$
157157

158158
---
159+
159160
### 总结
160161

161162
今天这道题目,三叶除了提供常规的、时间复杂度为 $O(n^2)$ 的朴素解法以外,还给你提供了关于「回文串」的最优解 Manacher 算法模板,建议有余力的同学可以背过。

LeetCode/131-140/131. 分割回文串(中等).md

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ Tag : 「回文串」、「回溯算法」、「动态规划」
66

77

88

9-
给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。
10-
11-
回文串 是正着读和反着读都一样的字符串。
9+
给你一个字符串 `s`,请你将 `s` 分割成一些子串,使每个子串都是 回文串 。
1210

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

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

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

2626
提示:
27-
* 1 <= s.length <= 16
28-
* s 仅由小写英文字母组成
27+
* $1 <= s.length <= 16$
28+
* `s` 仅由小写英文字母组成
2929

3030
---
3131

LeetCode/131-140/132. 分割回文串 II(困难).md

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Tag : 「回文串」、「线性 DP」
66

77

88

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

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

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

3030
提示:
31-
* 1 <= s.length <= 2000
32-
* s 仅由小写英文字母组成
31+
* $1 <= s.length <= 2000$
32+
* `s` 仅由小写英文字母组成
3333

3434
---
3535

LeetCode/1601-1610/1601. 最多可达成的换楼请求数目(困难).md

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Tag : 「二进制枚举」
66

77

88

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

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

@@ -68,7 +68,7 @@ Tag : 「二进制枚举」
6868

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

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

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

109109
---
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1769. 移动所有球到每个盒子所需的最小操作数]()** ,难度为 **中等**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
`n` 个盒子。给你一个长度为 `n` 的二进制字符串 `boxes`,其中 `boxes[i]` 的值为 `'0'` 表示第 `i` 个盒子是 空 的,而 `boxes[i]` 的值为 `'1'` 表示盒子里有 一个 小球。
10+
11+
在一步操作中,你可以将 一个 小球从某个盒子移动到一个与之相邻的盒子中。第 `i` 个盒子和第 `j` 个盒子相邻需满足 `abs(i - j) == 1`。注意,操作执行后,某些盒子中可能会存在不止一个小球。
12+
13+
返回一个长度为 `n` 的数组 `answer`,其中 `answer[i]` 是将所有小球移动到第 `i` 个盒子所需的 最小 操作数。
14+
15+
每个 `answer[i]` 都需要根据盒子的 初始状态 进行计算。
16+
17+
示例 1:
18+
```
19+
输入:boxes = "110"
20+
21+
输出:[1,1,3]
22+
23+
解释:每个盒子对应的最小操作数如下:
24+
1) 第 1 个盒子:将一个小球从第 2 个盒子移动到第 1 个盒子,需要 1 步操作。
25+
2) 第 2 个盒子:将一个小球从第 1 个盒子移动到第 2 个盒子,需要 1 步操作。
26+
3) 第 3 个盒子:将一个小球从第 1 个盒子移动到第 3 个盒子,需要 2 步操作。将一个小球从第 2 个盒子移动到第 3 个盒子,需要 1 步操作。共计 3 步操作。
27+
```
28+
示例 2:
29+
```
30+
输入:boxes = "001011"
31+
32+
输出:[11,8,5,4,3,4]
33+
```
34+
35+
提示:
36+
* $n = boxes.length$
37+
* $1 <= n <= 2000$
38+
* `boxes[i]``'0'``'1'`
39+
40+
---
41+
42+
### 模拟
43+
44+
预处理两个与 `boxes` 等长的数组 `l``r`:$l[i]$ 和 $r[i]$ 分别代表「将 $[0, i]$ 的小球移动到位置 $i$」以及「将 $[i, n - 1]$ 的小球移动到位置 $i$」所需要的步数。
45+
46+
所求的答案数组 `ans` 与数组 `l``r` 的关系为:$ans[i] = l[i] + r[i]$。
47+
48+
预处理两数组是简单的:分别从两个方向遍历 `boxes`,使用变量 `cur` 代表当前处理到的前缀/后缀的小球总个数,变量 `step` 代表将当前所有前缀/后缀小球移动到位置 $i$ 所需要的步数。
49+
50+
Java 代码:
51+
```Java
52+
class Solution {
53+
public int[] minOperations(String boxes) {
54+
int n = boxes.length();
55+
int[] l = new int[n + 10], r = new int[n + 10];
56+
for (int i = 0, cur = 0, step = 0; i < n; i++) {
57+
step += cur; cur += boxes.charAt(i) - '0'; l[i] = step;
58+
}
59+
for (int i = n - 1, cur = 0, step = 0; i >= 0; i--) {
60+
step += cur; cur += boxes.charAt(i) - '0'; r[i] = step;
61+
}
62+
int[] ans = new int[n];
63+
for (int i = 0; i < n; i++) ans[i] = l[i] + r[i];
64+
return ans;
65+
}
66+
}
67+
```
68+
TypeScript 代码:
69+
```TypeScript
70+
function minOperations(boxes: string): number[] {
71+
const n = boxes.length
72+
const l = new Array<number>(n + 10).fill(0), r = new Array<number>(n + 10).fill(0)
73+
for (let i = 0, cur = 0, step = 0; i < n; i++) {
74+
step += cur; cur += boxes[i] == '1' ? 1 : 0; l[i] = step;
75+
}
76+
for (let i = n - 1, cur = 0, step = 0; i >= 0; i--) {
77+
step += cur; cur += boxes[i] == '1' ? 1 : 0; r[i] = step;
78+
}
79+
const ans = new Array<number>(n).fill(0)
80+
for (let i = 0; i < n; i++) ans[i] = l[i] + r[i]
81+
return ans
82+
}
83+
```
84+
Python 代码:
85+
```Python
86+
class Solution:
87+
def minOperations(self, boxes: str) -> List[int]:
88+
n = len(boxes)
89+
l, r = [0] * n, [0] * n
90+
step, cur = 0, 0
91+
for i in range(n):
92+
step, cur = step + cur, cur + 1 if boxes[i] == '1' else cur
93+
l[i] = step
94+
step, cur = 0, 0
95+
for i in range(n - 1, -1, -1):
96+
step, cur = step + cur, cur + 1 if boxes[i] == '1' else cur
97+
r[i] = step
98+
return [l[i] + r[i] for i in range(n)]
99+
```
100+
* 时间复杂度:$O(n)$
101+
* 空间复杂度:$O(n)$
102+
103+
---
104+
105+
### 最后
106+
107+
这是我们「刷穿 LeetCode」系列文章的第 `No.1769` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
108+
109+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
110+
111+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
112+
113+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
114+

LeetCode/521-530/526. 优美的排列(中等).md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Tag : 「位运算」、「状压 DP」、「动态规划」
3131
```
3232

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

3636
---
3737

LeetCode/641-650/650. 只有两个键的键盘(中等).md

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

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

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

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

12-
给你一个数字 n ,你需要使用最少的操作次数,在记事本上输出 恰好 n 个 'A' 。返回能够打印出 n 个 'A' 的最少操作次数。
9+
最初记事本上只有一个字符 `'A'` 。你每次可以对这个记事本进行两种操作:
10+
11+
* `Copy All(复制全部)`:复制这个记事本中的所有字符(不允许仅复制部分字符)。
12+
* `Paste(粘贴)`:粘贴 上一次 复制的字符。
13+
14+
给你一个数字 `n` ,你需要使用最少的操作次数,在记事本上输出 恰好 `n` 个 `'A'` 。返回能够打印出 `n` 个 `'A'` 的最少操作次数。
1315

1416

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

3436
提示:
35-
* 1 <= n <= 1000
37+
* $1 <= n <= 1000$
3638

3739
---
3840

LeetCode/671-680/678. 有效的括号字符串(中等).md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

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

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

911
有效字符串具有如下规则:

0 commit comments

Comments
 (0)