Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0b00ea9

Browse files
committed
Deploying to main from @ 9a7155e 🚀
1 parent 07a6cca commit 0b00ea9

39 files changed

+2648
-68
lines changed

‎book/binary_search.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ function bsearch(arr, value) {
246246
<!-- prettier-ignore -->
247247
| 题号 | 标题 | 题解 | 标签 | 难度 | 力扣 |
248248
| :------: | :------ | :------: | :------ | :------: | :------: |
249-
| 875 | 爱吃香蕉的珂珂 | | [`数组`](/tag/array.md) [`二分查找`](/tag/binary-search.md) | 🟠 | [🀄️](https://leetcode.cn/problems/koko-eating-bananas) [🔗](https://leetcode.com/problems/koko-eating-bananas) |
249+
| 875 | 爱吃香蕉的珂珂 | [[]](/problem/0875.md) | [`数组`](/tag/array.md) [`二分查找`](/tag/binary-search.md) | 🟠 | [🀄️](https://leetcode.cn/problems/koko-eating-bananas) [🔗](https://leetcode.com/problems/koko-eating-bananas) |
250250
| 410 | 分割数组的最大值 | | [`贪心`](/tag/greedy.md) [`数组`](/tag/array.md) [`二分查找`](/tag/binary-search.md) `2+` | 🔴 | [🀄️](https://leetcode.cn/problems/split-array-largest-sum) [🔗](https://leetcode.com/problems/split-array-largest-sum) |
251251
| 209 | 长度最小的子数组 | [[]](/problem/0209.md) | [`数组`](/tag/array.md) [`二分查找`](/tag/binary-search.md) [`前缀和`](/tag/prefix-sum.md) `1+` | 🟠 | [🀄️](https://leetcode.cn/problems/minimum-size-subarray-sum) [🔗](https://leetcode.com/problems/minimum-size-subarray-sum) |
252252
| 658 | 找到 K 个最接近的元素 | | [`数组`](/tag/array.md) [`双指针`](/tag/two-pointers.md) [`二分查找`](/tag/binary-search.md) `3+` | 🟠 | [🀄️](https://leetcode.cn/problems/find-k-closest-elements) [🔗](https://leetcode.com/problems/find-k-closest-elements) |

‎image/790.png

31.2 KB
Loading[フレーム]

‎plan/company_list.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ headerDepth: 0
6363
| 133 | 克隆图 | [[]](/problem/0133.md) | [`深度优先搜索`](/tag/depth-first-search.md) [`广度优先搜索`](/tag/breadth-first-search.md) [``](/tag/graph.md) `1+` | 🟠 | [🀄️](https://leetcode.cn/problems/clone-graph) [🔗](https://leetcode.com/problems/clone-graph) | 20 |
6464
| 138 | 随机链表的复制 | [[]](/problem/0138.md) | [`哈希表`](/tag/hash-table.md) [`链表`](/tag/linked-list.md) | 🟠 | [🀄️](https://leetcode.cn/problems/copy-list-with-random-pointer) [🔗](https://leetcode.com/problems/copy-list-with-random-pointer) | 19 |
6565
| 124 | 二叉树中的最大路径和 | [[]](/problem/0124.md) | [``](/tag/tree.md) [`深度优先搜索`](/tag/depth-first-search.md) [`动态规划`](/tag/dynamic-programming.md) `1+` | 🔴 | [🀄️](https://leetcode.cn/problems/binary-tree-maximum-path-sum) [🔗](https://leetcode.com/problems/binary-tree-maximum-path-sum) | 19 |
66-
| 1268 | 搜索推荐系统 | | [`字典树`](/tag/trie.md) [`数组`](/tag/array.md) [`字符串`](/tag/string.md) `3+` | 🟠 | [🀄️](https://leetcode.cn/problems/search-suggestions-system) [🔗](https://leetcode.com/problems/search-suggestions-system) | 18 |
66+
| 1268 | 搜索推荐系统 | [[]](/problem/1268.md) | [`字典树`](/tag/trie.md) [`数组`](/tag/array.md) [`字符串`](/tag/string.md) `3+` | 🟠 | [🀄️](https://leetcode.cn/problems/search-suggestions-system) [🔗](https://leetcode.com/problems/search-suggestions-system) | 18 |
6767
| 953 | 验证外星语词典 | | [`数组`](/tag/array.md) [`哈希表`](/tag/hash-table.md) [`字符串`](/tag/string.md) | 🟢 | [🀄️](https://leetcode.cn/problems/verifying-an-alien-dictionary) [🔗](https://leetcode.com/problems/verifying-an-alien-dictionary) | 18 |
6868
| 973 | 最接近原点的 K 个点 | [[]](/problem/0973.md) | [`几何`](/tag/geometry.md) [`数组`](/tag/array.md) [`数学`](/tag/math.md) `4+` | 🟠 | [🀄️](https://leetcode.cn/problems/k-closest-points-to-origin) [🔗](https://leetcode.com/problems/k-closest-points-to-origin) | 18 |
6969
| 29 | 两数相除 | [[]](/problem/0029.md) | [`位运算`](/tag/bit-manipulation.md) [`数学`](/tag/math.md) | 🟠 | [🀄️](https://leetcode.cn/problems/divide-two-integers) [🔗](https://leetcode.com/problems/divide-two-integers) | 18 |

‎plan/contest_list.md

Lines changed: 12 additions & 12 deletions
Large diffs are not rendered by default.

‎plan/rabbit_list.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ headerDepth: 0
101101
| 74 | 搜索二维矩阵 | [[]](/problem/0074.md) | [`数组`](/tag/array.md) [`二分查找`](/tag/binary-search.md) [`矩阵`](/tag/matrix.md) | 🟠 | [🀄️](https://leetcode.cn/problems/search-a-2d-matrix) [🔗](https://leetcode.com/problems/search-a-2d-matrix) | 18 |
102102
| 1448 | 统计二叉树中好节点的数目 | [[]](/problem/1448.md) | [``](/tag/tree.md) [`深度优先搜索`](/tag/depth-first-search.md) [`广度优先搜索`](/tag/breadth-first-search.md) `1+` | 🟠 | [🀄️](https://leetcode.cn/problems/count-good-nodes-in-binary-tree) [🔗](https://leetcode.com/problems/count-good-nodes-in-binary-tree) | 18 |
103103
| 100 | 相同的树 | [[]](/problem/0100.md) | [``](/tag/tree.md) [`深度优先搜索`](/tag/depth-first-search.md) [`广度优先搜索`](/tag/breadth-first-search.md) `1+` | 🟢 | [🀄️](https://leetcode.cn/problems/same-tree) [🔗](https://leetcode.com/problems/same-tree) | 18 |
104-
| 1268 | 搜索推荐系统 | | [`字典树`](/tag/trie.md) [`数组`](/tag/array.md) [`字符串`](/tag/string.md) `3+` | 🟠 | [🀄️](https://leetcode.cn/problems/search-suggestions-system) [🔗](https://leetcode.com/problems/search-suggestions-system) | 18 |
104+
| 1268 | 搜索推荐系统 | [[]](/problem/1268.md) | [`字典树`](/tag/trie.md) [`数组`](/tag/array.md) [`字符串`](/tag/string.md) `3+` | 🟠 | [🀄️](https://leetcode.cn/problems/search-suggestions-system) [🔗](https://leetcode.com/problems/search-suggestions-system) | 18 |
105105
| 953 | 验证外星语词典 | | [`数组`](/tag/array.md) [`哈希表`](/tag/hash-table.md) [`字符串`](/tag/string.md) | 🟢 | [🀄️](https://leetcode.cn/problems/verifying-an-alien-dictionary) [🔗](https://leetcode.com/problems/verifying-an-alien-dictionary) | 18 |
106106
| 973 | 最接近原点的 K 个点 | [[]](/problem/0973.md) | [`几何`](/tag/geometry.md) [`数组`](/tag/array.md) [`数学`](/tag/math.md) `4+` | 🟠 | [🀄️](https://leetcode.cn/problems/k-closest-points-to-origin) [🔗](https://leetcode.com/problems/k-closest-points-to-origin) | 18 |
107107
| 348 | 设计井字棋 🔒 | | [`设计`](/tag/design.md) [`数组`](/tag/array.md) [`哈希表`](/tag/hash-table.md) `2+` | 🟠 | [🀄️](https://leetcode.cn/problems/design-tic-tac-toe) [🔗](https://leetcode.com/problems/design-tic-tac-toe) | 18 |

‎problem/0020.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,4 @@ var isValid = function (s) {
123123
| 301 | 删除无效的括号 | | [`广度优先搜索`](/tag/breadth-first-search.md) [`字符串`](/tag/string.md) [`回溯`](/tag/backtracking.md) | 🔴 | [🀄️](https://leetcode.cn/problems/remove-invalid-parentheses) [🔗](https://leetcode.com/problems/remove-invalid-parentheses) |
124124
| 1003 | 检查替换后的词是否有效 | | [``](/tag/stack.md) [`字符串`](/tag/string.md) | 🟠 | [🀄️](https://leetcode.cn/problems/check-if-word-is-valid-after-substitutions) [🔗](https://leetcode.com/problems/check-if-word-is-valid-after-substitutions) |
125125
| 2116 | 判断一个括号字符串是否有效 | | [``](/tag/stack.md) [`贪心`](/tag/greedy.md) [`字符串`](/tag/string.md) | 🟠 | [🀄️](https://leetcode.cn/problems/check-if-a-parentheses-string-can-be-valid) [🔗](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid) |
126-
| 2337 | 移动片段得到字符串 | | [`双指针`](/tag/two-pointers.md) [`字符串`](/tag/string.md) | 🟠 | [🀄️](https://leetcode.cn/problems/move-pieces-to-obtain-a-string) [🔗](https://leetcode.com/problems/move-pieces-to-obtain-a-string) |
126+
| 2337 | 移动片段得到字符串 | [[]](/problem/2337.md) | [`双指针`](/tag/two-pointers.md) [`字符串`](/tag/string.md) | 🟠 | [🀄️](https://leetcode.cn/problems/move-pieces-to-obtain-a-string) [🔗](https://leetcode.com/problems/move-pieces-to-obtain-a-string) |

‎problem/0041.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ var firstMissingPositive = function (nums) {
158158
| 287 | 寻找重复数 | [[]](/problem/0287.md) | [`位运算`](/tag/bit-manipulation.md) [`数组`](/tag/array.md) [`双指针`](/tag/two-pointers.md) `1+` | 🟠 | [🀄️](https://leetcode.cn/problems/find-the-duplicate-number) [🔗](https://leetcode.com/problems/find-the-duplicate-number) |
159159
| 448 | 找到所有数组中消失的数字 | | [`数组`](/tag/array.md) [`哈希表`](/tag/hash-table.md) | 🟢 | [🀄️](https://leetcode.cn/problems/find-all-numbers-disappeared-in-an-array) [🔗](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array) |
160160
| 765 | 情侣牵手 | | [`贪心`](/tag/greedy.md) [`深度优先搜索`](/tag/depth-first-search.md) [`广度优先搜索`](/tag/breadth-first-search.md) `2+` | 🔴 | [🀄️](https://leetcode.cn/problems/couples-holding-hands) [🔗](https://leetcode.com/problems/couples-holding-hands) |
161-
| 2336 | 无限集中的最小数字 | | [`设计`](/tag/design.md) [`哈希表`](/tag/hash-table.md) [`有序集合`](/tag/ordered-set.md) `1+` | 🟠 | [🀄️](https://leetcode.cn/problems/smallest-number-in-infinite-set) [🔗](https://leetcode.com/problems/smallest-number-in-infinite-set) |
162-
| 2554 | 从一个范围内选择最多整数 I | | [`贪心`](/tag/greedy.md) [`数组`](/tag/array.md) [`哈希表`](/tag/hash-table.md) `2+` | 🟠 | [🀄️](https://leetcode.cn/problems/maximum-number-of-integers-to-choose-from-a-range-i) [🔗](https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-i) |
161+
| 2336 | 无限集中的最小数字 | [[]](/problem/2336.md) | [`设计`](/tag/design.md) [`哈希表`](/tag/hash-table.md) [`有序集合`](/tag/ordered-set.md) `1+` | 🟠 | [🀄️](https://leetcode.cn/problems/smallest-number-in-infinite-set) [🔗](https://leetcode.com/problems/smallest-number-in-infinite-set) |
162+
| 2554 | 从一个范围内选择最多整数 I | [[]](/problem/2554.md) | [`贪心`](/tag/greedy.md) [`数组`](/tag/array.md) [`哈希表`](/tag/hash-table.md) `2+` | 🟠 | [🀄️](https://leetcode.cn/problems/maximum-number-of-integers-to-choose-from-a-range-i) [🔗](https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-i) |
163163
| 2557 | 从一个范围内选择最多整数 II 🔒 | | [`贪心`](/tag/greedy.md) [`数组`](/tag/array.md) [`二分查找`](/tag/binary-search.md) `1+` | 🟠 | [🀄️](https://leetcode.cn/problems/maximum-number-of-integers-to-choose-from-a-range-ii) [🔗](https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-ii) |
164164
| 2598 | 执行操作后的最大 MEX | | [`贪心`](/tag/greedy.md) [`数组`](/tag/array.md) [`哈希表`](/tag/hash-table.md) `1+` | 🟠 | [🀄️](https://leetcode.cn/problems/smallest-missing-non-negative-integer-after-operations) [🔗](https://leetcode.com/problems/smallest-missing-non-negative-integer-after-operations) |
165165
| 2996 | 大于等于顺序前缀和的最小缺失整数 | | [`数组`](/tag/array.md) [`哈希表`](/tag/hash-table.md) [`排序`](/tag/sorting.md) | 🟢 | [🀄️](https://leetcode.cn/problems/smallest-missing-integer-greater-than-sequential-prefix-sum) [🔗](https://leetcode.com/problems/smallest-missing-integer-greater-than-sequential-prefix-sum) |

‎problem/0392.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,4 @@ var left_bound = function (arr, target) {
176176
| 792 | 匹配子序列的单词数 | | [`字典树`](/tag/trie.md) [`数组`](/tag/array.md) [`哈希表`](/tag/hash-table.md) `4+` | 🟠 | [🀄️](https://leetcode.cn/problems/number-of-matching-subsequences) [🔗](https://leetcode.com/problems/number-of-matching-subsequences) |
177177
| 1055 | 形成字符串的最短路径 🔒 | | [`贪心`](/tag/greedy.md) [`双指针`](/tag/two-pointers.md) [`字符串`](/tag/string.md) `1+` | 🟠 | [🀄️](https://leetcode.cn/problems/shortest-way-to-form-string) [🔗](https://leetcode.com/problems/shortest-way-to-form-string) |
178178
| 2486 | 追加字符以获得子序列 | | [`贪心`](/tag/greedy.md) [`双指针`](/tag/two-pointers.md) [`字符串`](/tag/string.md) | 🟠 | [🀄️](https://leetcode.cn/problems/append-characters-to-string-to-make-subsequence) [🔗](https://leetcode.com/problems/append-characters-to-string-to-make-subsequence) |
179-
| 2825 | 循环增长使字符串子序列等于另一个字符串 | | [`双指针`](/tag/two-pointers.md) [`字符串`](/tag/string.md) | 🟠 | [🀄️](https://leetcode.cn/problems/make-string-a-subsequence-using-cyclic-increments) [🔗](https://leetcode.com/problems/make-string-a-subsequence-using-cyclic-increments) |
179+
| 2825 | 循环增长使字符串子序列等于另一个字符串 | [[]](/problem/2825.md) | [`双指针`](/tag/two-pointers.md) [`字符串`](/tag/string.md) | 🟠 | [🀄️](https://leetcode.cn/problems/make-string-a-subsequence-using-cyclic-increments) [🔗](https://leetcode.com/problems/make-string-a-subsequence-using-cyclic-increments) |

‎problem/0502.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,5 @@ var findMaximizedCapital = function (k, w, profits, capital) {
177177
<!-- prettier-ignore -->
178178
| 题号 | 标题 | 题解 | 标签 | 难度 | 力扣 |
179179
| :------: | :------ | :------: | :------ | :------: | :------: |
180-
| 2542 | 最大子序列的分数 | | [`贪心`](/tag/greedy.md) [`数组`](/tag/array.md) [`排序`](/tag/sorting.md) `1+` | 🟠 | [🀄️](https://leetcode.cn/problems/maximum-subsequence-score) [🔗](https://leetcode.com/problems/maximum-subsequence-score) |
180+
| 2542 | 最大子序列的分数 | [[]](/problem/2542.md) | [`贪心`](/tag/greedy.md) [`数组`](/tag/array.md) [`排序`](/tag/sorting.md) `1+` | 🟠 | [🀄️](https://leetcode.cn/problems/maximum-subsequence-score) [🔗](https://leetcode.com/problems/maximum-subsequence-score) |
181181
| 2813 | 子序列最大优雅度 | | [``](/tag/stack.md) [`贪心`](/tag/greedy.md) [`数组`](/tag/array.md) `3+` | 🔴 | [🀄️](https://leetcode.cn/problems/maximum-elegance-of-a-k-length-subsequence) [🔗](https://leetcode.com/problems/maximum-elegance-of-a-k-length-subsequence) |

‎problem/0790.md

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,105 @@ that exactly one of the tilings has both squares occupied by a tile.
8080

8181
## 解题思路
8282

83+
这道题的核心在于如何利用动态规划来枚举所有可能的铺法。
84+
85+
如图所示:
86+
87+
![](../image/790.png)
88+
89+
- `dp[0] = 1`:空棋盘有一种铺法。
90+
- `dp[1] = 1`:一个 `2 * 1` 的棋盘只能用一个竖直的多米诺骨牌铺满。
91+
- `dp[2] = 2`:两个竖直多米诺骨牌或两个水平多米诺骨牌。
92+
- `dp[3] = 5`:
93+
- `dp[3] = dp[2] + dp[1] + 2 * dp[0]`
94+
-`dp[2]` 后面加一个竖直的多米诺骨牌:`2`
95+
-`dp[1]` 后面加两个水平的多米诺骨牌:`1`
96+
-`dp[0]` 后面加两个 L 形托米诺骨牌:`2`
97+
- `dp[4] = 11`:
98+
- `dp[4] = dp[3] + dp[2] + 2 * dp[1] + 2 * dp[0]`
99+
-`dp[3]` 后面加一个竖直的多米诺骨牌:`5`
100+
-`dp[2]` 后面加两个水平的多米诺骨牌:`2`
101+
-`dp[1]` 后面加两个 L 形托米诺骨牌:`2`
102+
-`dp[0]` 后面加一个水平的多米诺骨牌和两个 L 形托米诺骨牌:`2`
103+
- `dp[5] = 24`:
104+
- `dp[5] = dp[4] + dp[3] + 2 * dp[2] + 2 * dp[1] + 2 * dp[0]`
105+
-`dp[4]` 后面加一个竖直的多米诺骨牌:`11`
106+
-`dp[3]` 后面加两个水平的多米诺骨牌:`5`
107+
-`dp[2]` 后面加两个 L 形托米诺骨牌:`4`
108+
-`dp[1]` 后面加一个水平的多米诺骨牌和两个 L 形托米诺骨牌:`2`
109+
-`dp[0]` 后面加两个水平的多米诺骨牌和两个 L 形托米诺骨牌:`2`
110+
111+
由此可以得到递推关系:
112+
113+
```
114+
dp[n] = dp[n-1] + dp[n-2] + 2 * (dp[n-3] + ... + d[0])
115+
= dp[n-1] + dp[n-2] + dp[n-3] + dp[n-3] + 2 * (dp[n-4] + ... + d[0])
116+
= dp[n-1] + dp[n-3] + (dp[n-2] + dp[n-3] + 2 * (dp[n-4] + ... + d[0]))
117+
= dp[n-1] + dp[n-3] + dp[n-1]
118+
= 2 * dp[n-1] + dp[n-3]
119+
```
120+
83121
#### 复杂度分析
84122

85-
- **时间复杂度**:`O()`,
86-
- **空间复杂度**:`O()`,
123+
- **时间复杂度**:`O(n)`,动态规划需要遍历 `n`
124+
125+
- **空间复杂度**:
126+
- 常规 DP:需要存储 `n + 1` 个状态,空间复杂度为 `O(n)`
127+
- 优化 DP:只需要三个变量,空间复杂度为 `O(1)`
87128

88129
## 代码
89130

131+
::: code-tabs
132+
@tab 动态规划
133+
90134
```javascript
135+
/**
136+
* @param {number} n
137+
* @return {number}
138+
*/
139+
var numTilings = function (n) {
140+
const MOD = 1e9 + 7;
141+
142+
let dp = new Array(n + 1).fill(0);
143+
dp[0] = 1;
144+
dp[1] = 1;
145+
dp[2] = 2;
146+
147+
for (let i = 3; i <= n; i++) {
148+
dp[i] = (2 * dp[i - 1] + dp[i - 3]) % MOD;
149+
}
150+
151+
return dp[n];
152+
};
153+
```
154+
155+
@tab 动态规划(空间优化)
91156

157+
```javascript
158+
/**
159+
* @param {number} n
160+
* @return {number}
161+
*/
162+
var numTilings = function (n) {
163+
const MOD = 1e9 + 7;
164+
165+
if (n === 0) return 1;
166+
if (n === 1) return 1;
167+
if (n === 2) return 2;
168+
169+
let dp0 = 1,
170+
dp1 = 1,
171+
dp2 = 2;
172+
173+
for (let i = 3; i <= n; i++) {
174+
let curr = (2 * dp2 + dp0) % MOD;
175+
dp0 = dp1;
176+
dp1 = dp2;
177+
dp2 = curr;
178+
}
179+
180+
return dp2;
181+
};
92182
```
183+
184+
:::

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /