返回介绍

solution / 2100-2199 / 2147.Number of Ways to Divide a Long Corridor / README_EN

发布于 2024-06-17 01:03:09 字数 6656 浏览 0 评论 0 收藏 0

2147. Number of Ways to Divide a Long Corridor

中文文档

Description

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'.

Solutions

Solution 1

class Solution:
  def numberOfWays(self, corridor: str) -> int:
    @cache
    def dfs(i, cnt):
      if i == n:
        return int(cnt == 2)
      cnt += corridor[i] == 'S'
      if cnt > 2:
        return 0
      ans = dfs(i + 1, cnt)
      if cnt == 2:
        ans += dfs(i + 1, 0)
        ans %= mod
      return ans

    n = len(corridor)
    mod = 10**9 + 7
    ans = dfs(0, 0)
    dfs.cache_clear()
    return ans
class Solution {
  private String s;
  private int n;
  private int[][] f;
  private static final int MOD = (int) 1e9 + 7;

  public int numberOfWays(String corridor) {
    s = corridor;
    n = s.length();
    f = new int[n][3];
    for (var e : f) {
      Arrays.fill(e, -1);
    }
    return dfs(0, 0);
  }

  private int dfs(int i, int cnt) {
    if (i == n) {
      return cnt == 2 ? 1 : 0;
    }
    cnt += s.charAt(i) == 'S' ? 1 : 0;
    if (cnt > 2) {
      return 0;
    }
    if (f[i][cnt] != -1) {
      return f[i][cnt];
    }
    int ans = dfs(i + 1, cnt);
    if (cnt == 2) {
      ans += dfs(i + 1, 0);
      ans %= MOD;
    }
    f[i][cnt] = ans;
    return ans;
  }
}
class Solution {
public:
  const int mod = 1e9 + 7;

  int numberOfWays(string corridor) {
    int n = corridor.size();
    vector<vector<int>> f(n, vector<int>(3, -1));
    function<int(int, int)> dfs;
    dfs = [&](int i, int cnt) {
      if (i == n) return cnt == 2 ? 1 : 0;
      cnt += corridor[i] == 'S';
      if (cnt > 2) return 0;
      if (f[i][cnt] != -1) return f[i][cnt];
      int ans = dfs(i + 1, cnt);
      if (cnt == 2) {
        ans += dfs(i + 1, 0);
        ans %= mod;
      }
      f[i][cnt] = ans;
      return ans;
    };
    return dfs(0, 0);
  }
};
func numberOfWays(corridor string) int {
  n := len(corridor)
  var mod int = 1e9 + 7
  f := make([][]int, n)
  for i := range f {
    f[i] = make([]int, 3)
    for j := range f[i] {
      f[i][j] = -1
    }
  }
  var dfs func(i, cnt int) int
  dfs = func(i, cnt int) int {
    if i == n {
      if cnt == 2 {
        return 1
      }
      return 0
    }
    if corridor[i] == 'S' {
      cnt++
    }
    if cnt > 2 {
      return 0
    }
    if f[i][cnt] != -1 {
      return f[i][cnt]
    }
    ans := dfs(i+1, cnt)
    if cnt == 2 {
      ans += dfs(i+1, 0)
      ans %= mod
    }
    f[i][cnt] = ans
    return ans
  }
  return dfs(0, 0)
}
function numberOfWays(corridor: string): number {
  const M: number = 1e9 + 7;
  const seatNumbers: number[] = [];

  for (let i = 0; i < corridor.length; i++) {
    if (corridor.charAt(i) === 'S') {
      seatNumbers.push(i);
    }
  }

  if (seatNumbers.length % 2 !== 0 || seatNumbers.length === 0) {
    return 0;
  }

  let result: number = 1;

  for (let i = 2; i < seatNumbers.length; i += 2) {
    result = (result * (seatNumbers[i] - seatNumbers[i - 1])) % M;
  }

  return result;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文