comments | difficulty | edit_url | rating | source | tags | |||
---|---|---|---|---|---|---|---|---|
true |
Hard |
1914 |
Biweekly Contest 70 Q4 |
|
Along a long library corridor, there is a line of seats and decorative plants. You are given a 0-indexed string corridor
of length n
consisting of letters 'S'
and 'P'
where each 'S'
represents a seat and each 'P'
represents a plant.
One room divider has already been installed to the left of index 0
, and another to the right of index n - 1
. Additional room dividers can be installed. For each position between indices i - 1
and i
(1 <= i <= n - 1
), at most one divider can be installed.
Divide the corridor into non-overlapping sections, where each section has exactly two seats with any number of plants. There may be multiple ways to perform the division. Two ways are different if there is a position with a room divider installed in the first way but not in the second way.
Return the number of ways to divide the corridor. Since the answer may be very large, return it modulo 109 + 7
. If there is no way, return 0
.
Example 1:
Input: corridor = "SSPPSPS" Output: 3 Explanation: There are 3 different ways to divide the corridor. The black bars in the above image indicate the two room dividers already installed. Note that in each of the ways, each section has exactly two seats.
Example 2:
Input: corridor = "PPSPSP" Output: 1 Explanation: There is only 1 way to divide the corridor, by not installing any additional dividers. Installing any would create some section that does not have exactly two seats.
Example 3:
Input: corridor = "S" Output: 0 Explanation: There is no way to divide the corridor because there will always be a section that does not have exactly two seats.
Constraints:
n == corridor.length
1 <= n <= 105
corridor[i]
is either'S'
or'P'
.
We design a function
The calculation process of the function
If
Otherwise, we need to consider the situation at the current position
- If
$\textit{corridor}[i] = \text{'S'}$ , it means the current position is a seat, and we increment$k$ by$1$ . - If
$k > 2$ , it means the number of screens placed at the current position exceeds$2$ , so return$0$ . - Otherwise, we can choose not to place a screen, i.e.,
$\textit{dfs}(i + 1, k)$ . If$k = 2$ , we can also choose to place a screen, i.e.,$\textit{dfs}(i + 1, 0)$ . We add the results of these two cases and take the result modulo$10^9 + 7$ , i.e.,$\textit{ans} = (\textit{ans} + \textit{dfs}(i + 1, k)) \bmod \text{mod}$ .
Finally, we return
The time complexity is
class Solution:
def numberOfWays(self, corridor: str) -> int:
@cache
def dfs(i: int, k: int) -> int:
if i >= len(corridor):
return int(k == 2)
k += int(corridor[i] == "S")
if k > 2:
return 0
ans = dfs(i + 1, k)
if k == 2:
ans = (ans + dfs(i + 1, 0)) % mod
return ans
mod = 10**9 + 7
ans = dfs(0, 0)
dfs.cache_clear()
return ans
class Solution {
private int n;
private char[] s;
private Integer[][] f;
private final int mod = (int) 1e9 + 7;
public int numberOfWays(String corridor) {
s = corridor.toCharArray();
n = s.length;
f = new Integer[n][3];
return dfs(0, 0);
}
private int dfs(int i, int k) {
if (i >= n) {
return k == 2 ? 1 : 0;
}
if (f[i][k] != null) {
return f[i][k];
}
k += s[i] == 'S' ? 1 : 0;
if (k > 2) {
return 0;
}
int ans = dfs(i + 1, k);
if (k == 2) {
ans = (ans + dfs(i + 1, 0)) % mod;
}
return f[i][k] = ans;
}
}
class Solution {
public:
int numberOfWays(string corridor) {
int n = corridor.size();
int f[n][3];
memset(f, -1, sizeof(f));
const int mod = 1e9 + 7;
auto dfs = [&](this auto&& dfs, int i, int k) -> int {
if (i >= n) {
return k == 2;
}
if (f[i][k] != -1) {
return f[i][k];
}
k += corridor[i] == 'S';
if (k > 2) {
return 0;
}
f[i][k] = dfs(i + 1, k);
if (k == 2) {
f[i][k] = (f[i][k] + dfs(i + 1, 0)) % mod;
}
return f[i][k];
};
return dfs(0, 0);
}
};
func numberOfWays(corridor string) int {
n := len(corridor)
f := make([][3]int, n)
for i := range f {
f[i] = [3]int{-1, -1, -1}
}
const mod = 1e9 + 7
var dfs func(int, int) int
dfs = func(i, k int) int {
if i >= n {
if k == 2 {
return 1
}
return 0
}
if f[i][k] != -1 {
return f[i][k]
}
if corridor[i] == 'S' {
k++
}
if k > 2 {
return 0
}
f[i][k] = dfs(i+1, k)
if k == 2 {
f[i][k] = (f[i][k] + dfs(i+1, 0)) % mod
}
return f[i][k]
}
return dfs(0, 0)
}
function numberOfWays(corridor: string): number {
const n = corridor.length;
const mod = 10 ** 9 + 7;
const f: number[][] = Array.from({ length: n }, () => Array(3).fill(-1));
const dfs = (i: number, k: number): number => {
if (i >= n) {
return k === 2 ? 1 : 0;
}
if (f[i][k] !== -1) {
return f[i][k];
}
if (corridor[i] === 'S') {
++k;
}
if (k > 2) {
return (f[i][k] = 0);
}
f[i][k] = dfs(i + 1, k);
if (k === 2) {
f[i][k] = (f[i][k] + dfs(i + 1, 0)) % mod;
}
return f[i][k];
};
return dfs(0, 0);
}
We can divide every two seats into a group. Between two adjacent groups of seats, if the distance between the last seat of the previous group and the first seat of the next group is
We traverse the corridor, using a variable
When we encounter a seat, we increment
Finally, if
The time complexity is
class Solution:
def numberOfWays(self, corridor: str) -> int:
mod = 10**9 + 7
ans, cnt, last = 1, 0, 0
for i, c in enumerate(corridor):
if c == "S":
cnt += 1
if cnt > 2 and cnt % 2:
ans = ans * (i - last) % mod
last = i
return ans if cnt and cnt % 2 == 0 else 0
class Solution {
public int numberOfWays(String corridor) {
final int mod = (int) 1e9 + 7;
long ans = 1, cnt = 0, last = 0;
for (int i = 0; i < corridor.length(); ++i) {
if (corridor.charAt(i) == 'S') {
if (++cnt > 2 && cnt % 2 == 1) {
ans = ans * (i - last) % mod;
}
last = i;
}
}
return cnt > 0 && cnt % 2 == 0 ? (int) ans : 0;
}
}
class Solution {
public:
int numberOfWays(string corridor) {
const int mod = 1e9 + 7;
long long ans = 1;
int cnt = 0, last = 0;
for (int i = 0; i < corridor.length(); ++i) {
if (corridor[i] == 'S') {
if (++cnt > 2 && cnt % 2) {
ans = ans * (i - last) % mod;
}
last = i;
}
}
return cnt > 0 && cnt % 2 == 0 ? ans : 0;
}
};
func numberOfWays(corridor string) int {
const mod int = 1e9 + 7
ans, cnt, last := 1, 0, 0
for i, c := range corridor {
if c == 'S' {
cnt++
if cnt > 2 && cnt%2 == 1 {
ans = ans * (i - last) % mod
}
last = i
}
}
if cnt > 0 && cnt%2 == 0 {
return ans
}
return 0
}
function numberOfWays(corridor: string): number {
const mod = 10 ** 9 + 7;
const n = corridor.length;
let [ans, cnt, last] = [1, 0, 0];
for (let i = 0; i < n; ++i) {
if (corridor[i] === 'S') {
if (++cnt > 2 && cnt % 2) {
ans = (ans * (i - last)) % mod;
}
last = i;
}
}
return cnt > 0 && cnt % 2 === 0 ? ans : 0;
}