zoukankan      html  css  js  c++  java
  • #Leetcode# 552. Student Attendance Record II

    https://leetcode.com/problems/student-attendance-record-ii/

    Given a positive integer n, return the number of all possible attendance records with length n, which will be regarded as rewardable. The answer may be very large, return it after mod 109 + 7.

    A student attendance record is a string that only contains the following three characters:

    1. 'A' : Absent.
    2. 'L' : Late.
    3. 'P' : Present.

    A record is regarded as rewardable if it doesn't contain more than one 'A' (absent) or more than two continuous 'L' (late).

    Example 1:

    Input: n = 2
    Output: 8 
    Explanation:
    There are 8 records with length 2 will be regarded as rewardable:
    "PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL"
    Only "AA" won't be regarded as rewardable owing to more than one absent times. 

    代码:

    class Solution {
    public:
        int checkRecord(int n) {
            const int mod = 1e9 + 7;
            const int maxn = 1e5 + 10;
            int dp[maxn][3][3];
            memset(dp, 0, sizeof(dp));
            for(int i = 0; i < 2; i ++) {
                for(int j = 0; j < 3; j ++) 
                    dp[0][i][j] = 1;
            }
            for(int i = 1; i <= n; i ++) {
                for(int j = 0; j < 2; j ++) {
                    for(int k = 0; k < 3; k ++) {
                        int ans = dp[i - 1][j][2];
                        if(j > 0) ans = (ans + dp[i - 1][j - 1][2]) % mod;
                        if(k > 0) ans = (ans + dp[i - 1][j][k - 1]) % mod;
                        dp[i][j][k] = ans % mod;
                    }
                }
            }
            return dp[n][1][2];
        }
    };
    

      dp[i][x][y] 存的是长度是 i 的时候 有 x 个 A y 个 连续的 L

     

  • 相关阅读:
    offsetheight和clientheight和scrollheight的区别以及offsetwidth和clientwidth和scrollwidth的区别
    响应时间控制
    浏览器兼容
    生成随机数
    递归加载目录
    用委托定义的冒泡排序法
    ref 与out
    二维数组与交错数组的理解
    C#学习
    Jquery选择器
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/10817909.html
Copyright © 2011-2022 走看看