[LeetCode]552. Student Attendance Record II

来源:互联网 发布:手机单机版进销存软件 编辑:程序博客网 时间:2024/06/06 13:25

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

A、P、L三种字母,其中A最多出现一次,L最多连续出现两次,问给定长度的字符串最多有多少种可能情况。结果取模1000000007。





dp数组内分为六种情况:

 A0L0:  A0L1:  A0L2: A1L0:  A1L1: A1L2:
AxLy表示当前字符串中一共x个A,结尾处一共连续y个L


public class Solution {    public int checkRecord(int n) {        int[] dp = {1, 1, 0, 1, 0, 0};        for (int i = 2; i <= n; i++) {            dp = new int[]{sum(dp, 0, 2), dp[0], dp[1], sum(dp, 0, 5), dp[3], dp[4]};        }        return sum(dp, 0, 5);    }    private int sum(int[] dp, int start, int end) {        final int MOD = 1000000007;        int res = 0;        for (int i = start; i <= end; i++) {            res = (res + dp[i]) % MOD;        }        return res;    }}


阅读全文
0 0
原创粉丝点击