Skip to content

Commit a28de4e

Browse files
author
lucifer
committed
fet: Consecutive Wins
1 parent 7f56e03 commit a28de4e

File tree

3 files changed

+111
-9
lines changed

3 files changed

+111
-9
lines changed

README.md

+10-9
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
243243
- [面试题 17.23. 最大黑方阵](./problems/max-black-square-lcci.md) 🆕
244244
- [Increasing Digits](./problems/Increasing-Digits.md) 👍 🆕
245245
- [Longest Contiguously Strictly Increasing Sublist After Deletion](./problems/Longest-Contiguously-Strictly-Increasing-Sublist-After-Deletion.md) 👍 🆕
246+
- [Consecutive Wins](./problems/consecutive-wins.md) 🆕
246247

247248
- [0002. 两数相加](./problems/2.add-two-numbers.md) 👍
248249
- [0003. 无重复字符的最长子串](./problems/3.longest-substring-without-repeating-characters.md)
@@ -303,9 +304,9 @@ leetcode 题解,记录自己的 leetcode 解题之路。
303304
- [0209. 长度最小的子数组](./problems/209.minimum-size-subarray-sum.md)
304305
- [0211. 添加与搜索单词 \* 数据结构设计](./problems/211.add-and-search-word-data-structure-design.md)
305306
- [0215. 数组中的第 K 个最大元素](./problems/215.kth-largest-element-in-an-array.md)
306-
- [0220. 存在重复元素 III](./problems/220.contains-duplicate-iii.md) 🆕
307+
- [0220. 存在重复元素 III](./problems/220.contains-duplicate-iii.md)
307308
- [0221. 最大正方形](./problems/221.maximal-square.md)
308-
- [0227. 基本计算器 II](./problems/227.basic-calculator-ii.md) 🆕
309+
- [0227. 基本计算器 II](./problems/227.basic-calculator-ii.md)
309310
- [0229. 求众数 II](./problems/229.majority-element-ii.md)
310311
- [0230. 二叉搜索树中第 K 小的元素](./problems/230.kth-smallest-element-in-a-bst.md)
311312
- [0236. 二叉树的最近公共祖先](./problems/236.lowest-common-ancestor-of-a-binary-tree.md)
@@ -315,7 +316,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
315316
- [0309. 最佳买卖股票时机含冷冻期](./problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md)
316317
- [0322. 零钱兑换](./problems/322.coin-change.md) 👍
317318
- [0328. 奇偶链表](./problems/328.odd-even-linked-list.md)
318-
- [0331. 验证二叉树的前序序列化](./problems/328.odd-even-linked-list.md) 🆕
319+
- [0331. 验证二叉树的前序序列化](./problems/328.odd-even-linked-list.md)
319320
- [0334. 递增的三元子序列](./problems/334.increasing-triplet-subsequence.md)
320321
- [0337. 打家劫舍 III](./problems/337.house-robber-iii.md)
321322
- [0343. 整数拆分](./problems/343.integer-break.md)
@@ -324,7 +325,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
324325
- [0380. 常数时间插入、删除和获取随机元素](./problems/380.insert-delete-getrandom-o1.md)
325326
- [0394. 字符串解码](./problems/394.decode-string.md) 91
326327
- [0416. 分割等和子集](./problems/416.partition-equal-subset-sum.md)
327-
- [0424. 替换后的最长重复字符](./problems/424.longest-repeating-character-replacement.md) 🆕
328+
- [0424. 替换后的最长重复字符](./problems/424.longest-repeating-character-replacement.md)
328329
- [0445. 两数相加 II](./problems/445.add-two-numbers-ii.md)
329330
- [0454. 四数相加 II](./problems/454.4-sum-ii.md)
330331
- [0464. 我能赢么](./problems/464.can-i-win.md)
@@ -344,7 +345,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
344345
- [0785. 判断二分图](./problems/785.is-graph-bipartite.md)
345346
- [0790. 多米诺和托米诺平铺](./problems/790.domino-and-tromino-tiling.md)
346347
- [0799. 香槟塔](./problems/799.champagne-tower.md) 🆕
347-
- [0801. 使序列递增的最小交换次数](./problems/801.minimum-swaps-to-make-sequences-increasing.md) 🆕
348+
- [0801. 使序列递增的最小交换次数](./problems/801.minimum-swaps-to-make-sequences-increasing.md)
348349
- [0816. 模糊坐标](./problems/816.ambiguous-coordinates.md)
349350
- [0820. 单词的压缩编码](./problems/820.short-encoding-of-words.md)
350351
- [0873. 最长的斐波那契子序列的长度](./problems/873.length-of-longest-fibonacci-subsequence.md) 🆕
@@ -355,21 +356,21 @@ leetcode 题解,记录自己的 leetcode 解题之路。
355356
- [0900. RLE 迭代器](./problems/900.rle-iterator.md)
356357
- [0911. 在线选举](./problems/911.online-election.md)
357358
- [0912. 排序数组](./problems/912.sort-an-array.md)
358-
- [0932. 漂亮数组](./problems/932.beautiful-array.md) 🆕
359+
- [0932. 漂亮数组](./problems/932.beautiful-array.md)
359360
- [0935. 骑士拨号器](./problems/935.knight-dialer.md)
360361
- [0947. 移除最多的同行或同列石头](./problems/947.most-stones-removed-with-same-row-or-column.md)
361-
- [0959. 由斜杠划分区域](./problems/959.regions-cut-by-slashes.md) 🆕
362+
- [0959. 由斜杠划分区域](./problems/959.regions-cut-by-slashes.md)
362363
- [0978. 最长湍流子数组](./problems/978.longest-turbulent-subarray.md)
363364
- [0987. 二叉树的垂序遍历](./problems/987.vertical-order-traversal-of-a-binary-tree.md) 91
364-
- [1004. 最大连续 1 的个数 III](./problems/1004.max-consecutive-ones-iii.md) 🆕
365+
- [1004. 最大连续 1 的个数 III](./problems/1004.max-consecutive-ones-iii.md)
365366
- [1011. 在 D 天内送达包裹的能力](./problems/1011.capacity-to-ship-packages-within-d-days.md)
366367
- [1014. 最佳观光组合](./problems/1014.best-sightseeing-pair.md)
367368
- [1015. 可被 K 整除的最小整数](./problems/1015.smallest-integer-divisible-by-k.md)
368369
- [1019. 链表中的下一个更大节点](./problems/1019.next-greater-node-in-linked-list.md)
369370
- [1020. 飞地的数量](./problems/1020.number-of-enclaves.md)
370371
- [1023. 驼峰式匹配](./problems/1023.camelcase-matching.md)
371372
- [1031. 两个非重叠子数组的最大和](./problems/1031.maximum-sum-of-two-non-overlapping-subarrays.md)
372-
- [1043. 分隔数组以得到最大和](./problems/1043.partition-array-for-maximum-sum.md) 🆕
373+
- [1043. 分隔数组以得到最大和](./problems/1043.partition-array-for-maximum-sum.md)
373374
- [1104. 二叉树寻路](./problems/1104.path-in-zigzag-labelled-binary-tree.md)
374375
- [1131.绝对值表达式的最大值](./problems/1131.maximum-of-absolute-value-expression.md)
375376
- [1186. 删除一次得到子数组最大和](./problems/1186.maximum-subarray-sum-with-one-deletion.md)

SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
- [面试题 17.23. 最大黑方阵](./problems/max-black-square-lcci.md)🆕
101101
- [Increasing Digits](./problems/Increasing-Digits.md) 👍 🆕
102102
- [Longest Contiguously Strictly Increasing Sublist After Deletion](./problems/Longest-Contiguously-Strictly-Increasing-Sublist-After-Deletion.md) 👍 🆕
103+
- [Consecutive Wins](./problems/consecutive-wins.md) 🆕
103104
- [0002. 两数相加](./problems/2.add-two-numbers.md)
104105
- [0003. 无重复字符的最长子串](./problems/3.longest-substring-without-repeating-characters.md)
105106
- [0005. 最长回文子串](./problems/5.longest-palindromic-substring.md)

problems/consecutive-wins.md

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# 题目地址(726.Consecutive Wins)
2+
3+
https://binarysearch.com/problems/Consecutive-Wins
4+
5+
## 题目描述
6+
7+
```
8+
You are given integers n and k. Given that n represents the number of games you will play, return the number of ways in which you win k or less games consecutively. Mod the result by 10 ** 9 + 7.
9+
10+
Constraints
11+
12+
n ≤ 10,000
13+
k ≤ 10
14+
Example 1
15+
Input
16+
n = 3
17+
k = 2
18+
Output
19+
7
20+
Explanation
21+
Here are the ways in which we can win 2 or fewer times consecutively:
22+
23+
"LLL"
24+
"WLL"
25+
"LWL"
26+
"LLW"
27+
"WWL"
28+
"LWW"
29+
30+
31+
```
32+
33+
## 前置知识
34+
35+
- 递归树
36+
- 动态规划
37+
38+
## 公司
39+
40+
- 暂无
41+
42+
## 思路
43+
44+
定义 f(i, j) 表示 i 次比赛连续赢 j 次的总的可能数目。 其实也就是**长度为 n - i 的字符串中连续 w 的次数不超过 j 的总的可能数**,其中 字符串中的字符只能是 L 或 W。
45+
46+
经过这样的定义之后,我们的答案应该是 f(0, 0)。
47+
48+
实际上,也就是问动态规划的转移方程是什么。
49+
50+
对于 f(0, 0),我们可以:
51+
52+
- 在末尾增加一个 L,也就是输一局。用公式表示就是 f(1, 0)
53+
- 在末尾增加一个 W,也就是赢一局。用公式表示就是 f(1, 1)
54+
55+
用图来表示就是如下的样子:
56+
57+
![图采用力扣加加刷题插件制作](https://tva1.sinaimg.cn/large/008eGmZEly1gotv04k40uj30jg0glmzp.jpg)
58+
59+
不是一般性,我们可以得出如下的转移方程:
60+
61+
$$
62+
f(i, j)=\left\{
63+
\begin{aligned}
64+
f(i+1, 0) + f(i+1, j+1) & & j < k \\
65+
f(i+1, 0) & & j = k \\
66+
\end{aligned}
67+
\right.
68+
$$
69+
70+
那么我们的目标其实就是计算图中叶子节点(绿色节点)的总个数。
71+
72+
> 注意 f(3,3) 是不合法的,我们不应该将其计算进去。
73+
74+
上面使我们的递归树代码,可以看出有很多重复的计算。这提示我们使用记忆化递归来解决。使用记忆化递归,总的时间复杂度 节点总数 \* 单个节点的操作数。树的节点总数是 n \* k,单个节点的操作是常数。故总的时间复杂度为 $O(n \* k),空间复杂度是使用的递归深度 + 记忆化使用的额外空间。其中递归深度是 $n$,记忆化的空间为 $n * k$,忽略低次项后空间复杂度为 $O(n * k)$
75+
76+
## 代码
77+
78+
代码支持:Python3
79+
80+
Python3 Code:
81+
82+
```py
83+
class Solution:
84+
def solve(self, n, k):
85+
@lru_cache(None)
86+
def dp(i, cnt):
87+
if i == n:
88+
return 1
89+
ans = dp(i + 1, 0) # place L
90+
if cnt < k:
91+
ans += dp(i + 1, cnt + 1) # place W if I can
92+
return ans
93+
94+
return dp(0, 0) % (10 ** 9 + 7)
95+
```
96+
97+
**复杂度分析**
98+
99+
- 时间复杂度:$O(n * k)$
100+
- 空间复杂度:$O(n * k)$

0 commit comments

Comments
 (0)