forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
981da86
commit 9015242
Showing
1 changed file
with
11 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
### 1223.Dice-Roll-Simulation | ||
|
||
本题属于一类常规的DP题。这类题目的特征就是“不能出现联系多少个XXX”。 | ||
|
||
以前做过一类更简化的版本,就是“不能出现连续两个相同的XXX”。那种情况下,定义dp[i][j]表示第i轮选择状态j,则```dp[i][j] = sum(dp[i-1][j'])```其中j'是所有的状态选择但不能是j。 | ||
|
||
这个题目更general,不能连续出现的次数是个变量,而且与j有关。于是我们可以定义dp[i][j][k]表示“第i轮选择状态j、并且结尾已经出现了连续k个状态j的方案数目”。显然我们可以分两种情况讨论: | ||
1. k==1,说明第i-1轮的时候可以选任何不同于j的状态j',并且第三个下标没有限制(只要不超过rollMax[j']的限制即可)。 | ||
2. k>1,说明第i-1轮的时候必须任然是j的状态,且第三个下标只能是k-1(表示结尾连续出现了k-1次) | ||
|
||
最终的结果就是dp[n][j][k]对于任意j和k的情况的总和。 |