Skip to content

feat: add solutions to lc problem: No.3320 #3640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +79,211 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3320.Co

<!-- solution:start -->

### 方法一
### 方法一:记忆化搜索

我们设计一个函数 $\textit{dfs}(i, j, k)$,其中 $i$ 表示从字符串 $s$ 的第 $i$ 个字符开始,目前 $\textit{Alice}$ 与 $\textit{Bob}$ 的分数差为 $j$,并且 $\textit{Bob}$ 上一次召唤的生物是 $k$,一共有多少种 $\textit{Bob}$ 的出招序列可以战胜 $\textit{Alice}$。

那么答案就是 $\textit{dfs}(0, 0, -1)$。其中 $-1$ 表示 $\textit{Bob}$ 还没有召唤过生物。在除了 $\textit{Python}$ 之外的语言中,由于分数差可能为负数,我们可以将分数差加上 $n$,这样就可以保证分数差为非负数。

函数 $\textit{dfs}(i, j, k)$ 的计算过程如下:

- 如果 $n - i \leq j$,那么剩余的回合数不足以使 $\textit{Bob}$ 的分数超过 $\textit{Alice}$ 的分数,此时返回 $0$。
- 如果 $i \geq n$,那么所有回合已经结束,如果 $\textit{Bob}$ 的分数小于 $0$,那么返回 $1$,否则返回 $0$。
- 否则,我们枚举 $\textit{Bob}$ 这一回合召唤的生物,如果这一回合召唤的生物与上一回合召唤的生物相同,那么这一回合 $\textit{Bob}$ 无法获胜,直接跳过。否则,我们递归计算 $\textit{dfs}(i + 1, j + \textit{calc}(d[s[i]], l), l)$,其中 $\textit{calc}(x, y)$ 表示 $x$ 与 $y$ 之间的胜负关系,而 $d$ 是一个映射,将字符映射到 $\textit{012}$。我们将所有的结果相加并对 $10^9 + 7$ 取模。

时间复杂度 $O(n^2 \times k^2)$,其中 $n$ 是字符串 $s$ 的长度,而 $k$ 表示字符集的大小。空间复杂度 $O(n^2 \times k)$。

<!-- tabs:start -->

#### Python3

```python

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
```

#### Java

```java

class Solution {
private int n;
private char[] s;
private int[] d = new int[26];
private Integer[][][] f;
private final int mod = (int) 1e9 + 7;

public int countWinningSequences(String s) {
d['W' - 'A'] = 1;
d['E' - 'A'] = 2;
this.s = s.toCharArray();
n = this.s.length;
f = new Integer[n][n + n + 1][4];
return dfs(0, n, 3);
}

private int dfs(int i, int j, int k) {
if (n - i <= j - n) {
return 0;
}
if (i >= n) {
return j - n < 0 ? 1 : 0;
}
if (f[i][j][k] != null) {
return f[i][j][k];
}

int ans = 0;
for (int l = 0; l < 3; ++l) {
if (l == k) {
continue;
}
ans = (ans + dfs(i + 1, j + calc(d[s[i] - 'A'], l), l)) % mod;
}
return f[i][j][k] = ans;
}

private int calc(int x, int y) {
if (x == y) {
return 0;
}
if (x < y) {
return x == 0 && y == 2 ? 1 : -1;
}
return x == 2 && y == 0 ? -1 : 1;
}
}
```

#### C++

```cpp

class Solution {
public:
int countWinningSequences(string s) {
int n = s.size();
int d[26]{};
d['W' - 'A'] = 1;
d['E' - 'A'] = 2;
int f[n][n + n + 1][4];
memset(f, -1, sizeof(f));
auto calc = [](int x, int y) -> int {
if (x == y) {
return 0;
}
if (x < y) {
return x == 0 && y == 2 ? 1 : -1;
}
return x == 2 && y == 0 ? -1 : 1;
};
const int mod = 1e9 + 7;
auto dfs = [&](auto&& dfs, int i, int j, int k) -> int {
if (n - i <= j - n) {
return 0;
}
if (i >= n) {
return j - n < 0 ? 1 : 0;
}
if (f[i][j][k] != -1) {
return f[i][j][k];
}
int ans = 0;
for (int l = 0; l < 3; ++l) {
if (l == k) {
continue;
}
ans = (ans + dfs(dfs, i + 1, j + calc(d[s[i] - 'A'], l), l)) % mod;
}
return f[i][j][k] = ans;
};
return dfs(dfs, 0, n, 3);
}
};
```

#### Go

```go

func countWinningSequences(s string) int {
const mod int = 1e9 + 7
d := [26]int{}
d['W'-'A'] = 1
d['E'-'A'] = 2
n := len(s)
f := make([][][4]int, n)
for i := range f {
f[i] = make([][4]int, n+n+1)
for j := range f[i] {
for k := range f[i][j] {
f[i][j][k] = -1
}
}
}
calc := func(x, y int) int {
if x == y {
return 0
}
if x < y {
if x == 0 && y == 2 {
return 1
}
return -1
}
if x == 2 && y == 0 {
return -1
}
return 1
}
var dfs func(int, int, int) int
dfs = func(i, j, k int) int {
if n-i <= j-n {
return 0
}
if i >= n {
if j-n < 0 {
return 1
}
return 0
}
if v := f[i][j][k]; v != -1 {
return v
}
ans := 0
for l := 0; l < 3; l++ {
if l == k {
continue
}
ans = (ans + dfs(i+1, j+calc(d[s[i]-'A'], l), l)) % mod
}
f[i][j][k] = ans
return ans
}
return dfs(0, n, 3)
}
```

<!-- tabs:end -->
Expand Down
Loading
Loading