LeetCode 题解工作台
统计能获胜的出招序列数
Alice 和 Bob 正在玩一个幻想战斗游戏,游戏共有 n 回合,每回合双方各自都会召唤一个魔法生物:火龙( F )、水蛇( W )或地精( E )。每回合中,双方 同时 召唤魔法生物,并根据以下规则得分: 如果一方召唤火龙而另一方召唤地精,召唤 火龙 的玩家将获得一分。 如果一方召唤水蛇而另一方…
2
题型
4
代码语言
3
相关题
当前训练重点
困难 · 状态·转移·动态规划
答案摘要
我们设计一个函数 $\textit{dfs}(i, j, k)$,其中 表示从字符串 的第 个字符开始,目前 与 的分数差为 ,并且 上一次召唤的生物是 ,一共有多少种 的出招序列可以战胜 。 那么答案就是 $\textit{dfs}(0, 0, -1)$。其中 表示 还没有召唤过生物。在除了 之外的语言中,由于分数差可能为负数,我们可以将分数差加上 ,这样就可以保证分数差为非…
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路
题目描述
Alice 和 Bob 正在玩一个幻想战斗游戏,游戏共有 n 回合,每回合双方各自都会召唤一个魔法生物:火龙(F)、水蛇(W)或地精(E)。每回合中,双方 同时 召唤魔法生物,并根据以下规则得分:
- 如果一方召唤火龙而另一方召唤地精,召唤 火龙 的玩家将获得一分。
- 如果一方召唤水蛇而另一方召唤火龙,召唤 水蛇 的玩家将获得一分。
- 如果一方召唤地精而另一方召唤水蛇,召唤 地精 的玩家将获得一分。
- 如果双方召唤相同的生物,那么两个玩家都不会获得分数。
给你一个字符串 s,包含 n 个字符 'F'、'W' 和 'E',代表 Alice 每回合召唤的生物序列:
- 如果
s[i] == 'F',Alice 召唤火龙。 - 如果
s[i] == 'W',Alice 召唤水蛇。 - 如果
s[i] == 'E',Alice 召唤地精。
Bob 的出招序列未知,但保证 Bob 不会在连续两个回合中召唤相同的生物。如果在 n 轮后 Bob 获得的总分 严格大于 Alice 的总分,则 Bob 战胜 Alice。
返回 Bob 可以用来战胜 Alice 的不同出招序列的数量。
由于答案可能非常大,请返回答案对 109 + 7 取余 后的结果。
示例 1:
输入: s = "FFF"
输出: 3
解释:
Bob 可以通过以下 3 种出招序列战胜 Alice:"WFW"、"FWF" 或 "WEW"。注意,其他如 "WWE" 或 "EWW" 的出招序列是无效的,因为 Bob 不能在连续两个回合中使用相同的生物。
示例 2:
输入: s = "FWEFW"
输出: 18
解释:
Bob 可以通过以下出招序列战胜 Alice:"FWFWF"、"FWFWE"、"FWEFE"、"FWEWE"、"FEFWF"、"FEFWE"、"FEFEW"、"FEWFE"、"WFEFE"、"WFEWE"、"WEFWF"、"WEFWE"、"WEFEF"、"WEFEW"、"WEWFW"、"WEWFE"、"EWFWE" 或 "EWEWE"。
提示:
1 <= s.length <= 1000s[i]是'F'、'W'或'E'中的一个。
解题思路
方法一:记忆化搜索
我们设计一个函数 ,其中 表示从字符串 的第 个字符开始,目前 与 的分数差为 ,并且 上一次召唤的生物是 ,一共有多少种 的出招序列可以战胜 。
那么答案就是 。其中 表示 还没有召唤过生物。在除了 之外的语言中,由于分数差可能为负数,我们可以将分数差加上 ,这样就可以保证分数差为非负数。
函数 的计算过程如下:
- 如果 ,那么剩余的回合数不足以使 的分数超过 的分数,此时返回 。
- 如果 ,那么所有回合已经结束,如果 的分数小于 ,那么返回 ,否则返回 。
- 否则,我们枚举 这一回合召唤的生物,如果这一回合召唤的生物与上一回合召唤的生物相同,那么这一回合 无法获胜,直接跳过。否则,我们递归计算 ,其中 表示 与 之间的胜负关系,而 是一个映射,将字符映射到 。我们将所有的结果相加并对 取模。
时间复杂度 ,其中 是字符串 的长度,而 表示字符集的大小。空间复杂度 。
class Solution:
def countWinningSequences(self, s: str) -> int:
def calc(x: int, y: int) -> int:
if x == y:
return 0
if x < y:
return 1 if x == 0 and y == 2 else -1
return -1 if x == 2 and y == 0 else 1
@cache
def dfs(i: int, j: int, k: int) -> int:
if len(s) - i <= j:
return 0
if i >= len(s):
return int(j < 0)
res = 0
for l in range(3):
if l == k:
continue
res = (res + dfs(i + 1, j + calc(d[s[i]], l), l)) % mod
return res
mod = 10**9 + 7
d = {"F": 0, "W": 1, "E": 2}
ans = dfs(0, 0, -1)
dfs.cache_clear()
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity depends on the approach to state transitions and the number of rounds, but it can be reduced to O(n) where n is the length of Alice’s sequence. Space complexity can vary, but typically O(n) for storing the dynamic programming table and intermediate states. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Does the candidate understand state transitions and dynamic programming?
- question_mark
Can the candidate break down a problem into subproblems using DP?
- question_mark
Is the candidate aware of how to optimize the space complexity of a DP approach?
常见陷阱
外企场景- error
Not properly handling the state transitions between consecutive rounds for Bob.
- error
Incorrectly counting the number of valid sequences or forgetting to compare Bob’s points against Alice’s.
- error
Using an inefficient approach that results in a high time complexity, especially with larger input sizes.
进阶变体
外企场景- arrow_right_alt
What if the number of creatures is expanded to more than three types?
- arrow_right_alt
What if Bob is allowed to repeat moves in consecutive rounds?
- arrow_right_alt
What if the rounds are not fixed and are dynamically generated?