Count The Number of Winning Sequences Problem
Description
LeetCode Problem 3320.
Alice and Bob are playing a fantasy battle game consisting of n rounds where they summon one of three magical creatures each round: a Fire Dragon, a Water Serpent, or an Earth Golem. In each round, players simultaneously summon their creature and are awarded points as follows:
- If one player summons a Fire Dragon and the other summons an Earth Golem, the player who summoned the Fire Dragon is awarded a point.
- If one player summons a Water Serpent and the other summons a Fire Dragon, the player who summoned the Water Serpent is awarded a point.
- If one player summons an Earth Golem and the other summons a Water Serpent, the player who summoned the Earth Golem is awarded a point.
- If both players summon the same creature, no player is awarded a point.
You are given a string s consisting of n characters 'F', 'W', and 'E', representing the sequence of creatures Alice will summon in each round:
- If
s[i] == 'F', Alice summons a Fire Dragon. - If
s[i] == 'W', Alice summons a Water Serpent. - If
s[i] == 'E', Alice summons an Earth Golem.
Bob’s sequence of moves is unknown, but it is guaranteed that Bob will never summon the same creature in two consecutive rounds. Bob beats Alice if the total number of points awarded to Bob after n rounds is strictly greater than the points awarded to Alice.
Return the number of distinct sequences Bob can use to beat Alice.
Since the answer may be very large, return it modulo 10^9 + 7.
Example 1:
Input: s = "FFF"
Output: 3
Explanation:
Bob can beat Alice by making one of the following sequences of moves: "WFW", "FWF", or "WEW". Note that other winning sequences like "WWE" or "EWW" are invalid since Bob cannot make the same move twice in a row.
Example 2:
Input: s = "FWEFW"
Output: 18
Explanation:
Bob can beat Alice by making one of the following sequences of moves: "FWFWF", "FWFWE", "FWEFE", "FWEWE", "FEFWF", "FEFWE", "FEFEW", "FEWFE", "WFEFE", "WFEWE", "WEFWF", "WEFWE", "WEFEF", "WEFEW", "WEWFW", "WEWFE", "EWFWE", or "EWEWE".
Constraints:
1 <= s.length <= 1000s[i]is one of'F','W', or'E'.
Solution
Approach: Memoized Search
Define dfs(i, j, k): we are at round i, Alice's score minus Bob's is currently j, and Bob's previous creature was k. It returns the number of ways for Bob to play the remaining rounds and finish ahead. The answer is dfs(0, 0, -1), where -1 means Bob has not played yet. In languages without negative array indexes, shift j by n.
- If the rounds left (
n - i) cannot make up the deficit, that isn - i <= j, return0. - If
i == n, all rounds are done: return1ifj < 0(Bob is ahead) and0otherwise. - Otherwise try each of the three creatures for Bob except the one he played last round. The round's score change comes from a small win table
calc(Alice's creature, Bob's creature)over F, W, E mapped to 0, 1, 2. Sum the results ofdfs(i + 1, j + calc(...), l)modulo 10^9 + 7.
Complexity: O(n^2) states times a constant number of creatures, so O(n^2) time and space for n rounds, since there are O(n) rounds, O(n) score differences, and a constant 3 creatures.
from functools import lru_cache
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
@lru_cache(None)
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