返回介绍

solution / 0500-0599 / 0552.Student Attendance Record II / README_EN

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

552. Student Attendance Record II

中文文档

Description

An attendance record for a student can be represented as a string where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:

  • 'A': Absent.
  • 'L': Late.
  • 'P': Present.

Any student is eligible for an attendance award if they meet both of the following criteria:

  • The student was absent ('A') for strictly fewer than 2 days total.
  • The student was never late ('L') for 3 or more consecutive days.

Given an integer n, return _the number of possible attendance records of length_ n_ that make a student eligible for an attendance award. The answer may be very large, so return it modulo _109 + 7.

 

Example 1:

Input: n = 2
Output: 8
Explanation: There are 8 records with length 2 that are eligible for an award:
"PP", "AP", "PA", "LP", "PL", "AL", "LA", "LL"
Only "AA" is not eligible because there are 2 absences (there need to be fewer than 2).

Example 2:

Input: n = 1
Output: 3

Example 3:

Input: n = 10101
Output: 183236316

 

Constraints:

  • 1 <= n <= 105

Solutions

Solution 1

class Solution:
  def checkRecord(self, n: int) -> int:
    @cache
    def dfs(i, j, k):
      if i >= n:
        return 1
      ans = 0
      if j == 0:
        ans += dfs(i + 1, j + 1, 0)
      if k < 2:
        ans += dfs(i + 1, j, k + 1)
      ans += dfs(i + 1, j, 0)
      return ans % mod

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

  public int checkRecord(int n) {
    this.n = n;
    f = new Integer[n][2][3];
    return dfs(0, 0, 0);
  }

  private int dfs(int i, int j, int k) {
    if (i >= n) {
      return 1;
    }
    if (f[i][j][k] != null) {
      return f[i][j][k];
    }
    int ans = dfs(i + 1, j, 0);
    if (j == 0) {
      ans = (ans + dfs(i + 1, j + 1, 0)) % mod;
    }
    if (k < 2) {
      ans = (ans + dfs(i + 1, j, k + 1)) % mod;
    }
    return f[i][j][k] = ans;
  }
}
int f[100010][2][3];
const int mod = 1e9 + 7;

class Solution {
public:
  int checkRecord(int n) {
    this->n = n;
    memset(f, -1, sizeof(f));
    return dfs(0, 0, 0);
  }

  int dfs(int i, int j, int k) {
    if (i >= n) {
      return 1;
    }
    if (f[i][j][k] != -1) {
      return f[i][j][k];
    }
    int ans = dfs(i + 1, j, 0);
    if (j == 0) {
      ans = (ans + dfs(i + 1, j + 1, 0)) % mod;
    }
    if (k < 2) {
      ans = (ans + dfs(i + 1, j, k + 1)) % mod;
    }
    return f[i][j][k] = ans;
  }

private:
  int n;
};
func checkRecord(n int) int {
  f := make([][][]int, n)
  for i := range f {
    f[i] = make([][]int, 2)
    for j := range f[i] {
      f[i][j] = make([]int, 3)
      for k := range f[i][j] {
        f[i][j][k] = -1
      }
    }
  }
  const mod = 1e9 + 7
  var dfs func(i, j, k int) int
  dfs = func(i, j, k int) int {
    if i >= n {
      return 1
    }
    if f[i][j][k] != -1 {
      return f[i][j][k]
    }
    ans := dfs(i+1, j, 0)
    if j == 0 {
      ans = (ans + dfs(i+1, j+1, 0)) % mod
    }
    if k < 2 {
      ans = (ans + dfs(i+1, j, k+1)) % mod
    }
    f[i][j][k] = ans
    return ans
  }
  return dfs(0, 0, 0)
}

Solution 2

class Solution:
  def checkRecord(self, n: int) -> int:
    mod = int(1e9 + 7)
    dp = [[[0, 0, 0], [0, 0, 0]] for _ in range(n)]

    # base case
    dp[0][0][0] = dp[0][0][1] = dp[0][1][0] = 1

    for i in range(1, n):
      # A
      dp[i][1][0] = (dp[i - 1][0][0] + dp[i - 1][0][1] + dp[i - 1][0][2]) % mod
      # L
      dp[i][0][1] = dp[i - 1][0][0]
      dp[i][0][2] = dp[i - 1][0][1]
      dp[i][1][1] = dp[i - 1][1][0]
      dp[i][1][2] = dp[i - 1][1][1]
      # P
      dp[i][0][0] = (dp[i - 1][0][0] + dp[i - 1][0][1] + dp[i - 1][0][2]) % mod
      dp[i][1][0] = (
        dp[i][1][0] + dp[i - 1][1][0] + dp[i - 1][1][1] + dp[i - 1][1][2]
      ) % mod

    ans = 0
    for j in range(2):
      for k in range(3):
        ans = (ans + dp[n - 1][j][k]) % mod
    return ans
class Solution {
  private static final int MOD = 1000000007;

  public int checkRecord(int n) {
    long[][][] dp = new long[n][2][3];

    // base case
    dp[0][0][0] = 1;
    dp[0][0][1] = 1;
    dp[0][1][0] = 1;

    for (int i = 1; i < n; i++) {
      // A
      dp[i][1][0] = (dp[i - 1][0][0] + dp[i - 1][0][1] + dp[i - 1][0][2]) % MOD;
      // L
      dp[i][0][1] = dp[i - 1][0][0];
      dp[i][0][2] = dp[i - 1][0][1];
      dp[i][1][1] = dp[i - 1][1][0];
      dp[i][1][2] = dp[i - 1][1][1];
      // P
      dp[i][0][0] = (dp[i - 1][0][0] + dp[i - 1][0][1] + dp[i - 1][0][2]) % MOD;
      dp[i][1][0] = (dp[i][1][0] + dp[i - 1][1][0] + dp[i - 1][1][1] + dp[i - 1][1][2]) % MOD;
    }

    long ans = 0;
    for (int j = 0; j < 2; j++) {
      for (int k = 0; k < 3; k++) {
        ans = (ans + dp[n - 1][j][k]) % MOD;
      }
    }
    return (int) ans;
  }
}
constexpr int MOD = 1e9 + 7;

class Solution {
public:
  int checkRecord(int n) {
    using ll = long long;
    vector<vector<vector<ll>>> dp(n, vector<vector<ll>>(2, vector<ll>(3)));

    // base case
    dp[0][0][0] = dp[0][0][1] = dp[0][1][0] = 1;

    for (int i = 1; i < n; ++i) {
      // A
      dp[i][1][0] = (dp[i - 1][0][0] + dp[i - 1][0][1] + dp[i - 1][0][2]) % MOD;
      // L
      dp[i][0][1] = dp[i - 1][0][0];
      dp[i][0][2] = dp[i - 1][0][1];
      dp[i][1][1] = dp[i - 1][1][0];
      dp[i][1][2] = dp[i - 1][1][1];
      // P
      dp[i][0][0] = (dp[i - 1][0][0] + dp[i - 1][0][1] + dp[i - 1][0][2]) % MOD;
      dp[i][1][0] = (dp[i][1][0] + dp[i - 1][1][0] + dp[i - 1][1][1] + dp[i - 1][1][2]) % MOD;
    }

    ll ans = 0;
    for (int j = 0; j < 2; ++j) {
      for (int k = 0; k < 3; ++k) {
        ans = (ans + dp[n - 1][j][k]) % MOD;
      }
    }
    return ans;
  }
};
const _mod int = 1e9 + 7

func checkRecord(n int) int {
  dp := make([][][]int, n)
  for i := 0; i < n; i++ {
    dp[i] = make([][]int, 2)
    for j := 0; j < 2; j++ {
      dp[i][j] = make([]int, 3)
    }
  }

  // base case
  dp[0][0][0] = 1
  dp[0][0][1] = 1
  dp[0][1][0] = 1

  for i := 1; i < n; i++ {
    // A
    dp[i][1][0] = (dp[i-1][0][0] + dp[i-1][0][1] + dp[i-1][0][2]) % _mod
    // L
    dp[i][0][1] = dp[i-1][0][0]
    dp[i][0][2] = dp[i-1][0][1]
    dp[i][1][1] = dp[i-1][1][0]
    dp[i][1][2] = dp[i-1][1][1]
    // P
    dp[i][0][0] = (dp[i-1][0][0] + dp[i-1][0][1] + dp[i-1][0][2]) % _mod
    dp[i][1][0] = (dp[i][1][0] + dp[i-1][1][0] + dp[i-1][1][1] + dp[i-1][1][2]) % _mod
  }

  var ans int
  for j := 0; j < 2; j++ {
    for k := 0; k < 3; k++ {
      ans = (ans + dp[n-1][j][k]) % _mod
    }
  }
  return ans
}

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

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

发布评论

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