首页 > 其他分享 >552.Student Attendance Record II (Hard)

552.Student Attendance Record II (Hard)

时间:2023-06-18 16:55:56浏览次数:43  
标签:Attendance dp3 dp2 Hard 552 return day dp mod

Description

552. Student Attendance Record II (Hard)

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 `10⁹

  • 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 <= 10⁵

Solution

We denote dp2[i][0] as the number of cases of Late on the i + 1th day, dp2[i][1] as the number of cases of Late on the i + 1th day, when there are only two cases: Late and Present.

  • dp2[i][1] = dp2[i - 1][0] + dp2[i - 1][1];
  • dp2[i][0] = dp2[i - 1][1] + dp2[i - 1][0] - dp2[n - 3][1];

We denote dp[i][0] as the number of cases of Late on the i + 1th day, dp[i][1] as the number of cases of Late on the i + 1th day, dp[i][2] as the number of cases of Absent on the i + 1th day.

  • dp[i][1] = dp[i - 1][0] + dp[i - 1][1] + dp[i - 1][2];
  • dp[i][2] = dp2[i - 1][0] + dp2[i - 1][1];
  • dp[i][0] = dp[i - 1][1] + dp[i - 1][2] + dp[i - 1][0] - (dp[i - 3][1] + dp[i - 3][2]);

Code

class Solution {
  public:
    int checkRecord(int n) {
        if (n == 1)
            return 3;
        if (n == 2)
            return 8;
        if (n == 3)
            return 19;
        vector<vector<long long>> dp3(n + 1, vector<long long>(3, 0));
        vector<vector<long long>> dp2(n, vector<long long>(2, 0));
        int mod = 1000000007;
        dp2[0][0] = dp2[0][1] = 1;
        dp2[1][0] = dp2[1][1] = 2;
        dp2[2][0] = 3;
        dp2[2][1] = 4;
        for (int i = 3; i < n; i++) {
            dp2[i][1] = (dp2[i - 1][0] % mod + dp2[i - 1][1] % mod) % mod;
            dp2[i][0] = (dp2[i - 1][1] % mod + (dp2[i - 1][0] - dp2[i - 3][1] + mod) % mod) % mod;
        }
        dp3[0][1] = dp3[0][0] = dp3[0][2] = 1;
        dp3[1][0] = 3, dp3[1][1] = 3, dp3[1][2] = 2;
        dp3[2][0] = 3 + 2 + 3 - 1, dp3[2][1] = 8, dp3[2][2] = 4;
        for (int i = 1; i < n; i++) {
            dp3[i][2] = (dp2[i - 1][0] % mod + dp2[i - 1][1] % mod) % mod;
        }
        for (int i = 3; i < n; i++) {
            dp3[i][0] = (dp3[i - 1][1] % mod + dp3[i - 1][2] % mod + (dp3[i - 1][0] - (dp3[i - 3][1] + dp3[i - 3][2]) + mod) % mod) % mod;
            dp3[i][1] = (dp3[i - 1][1] % mod + dp3[i - 1][0] % mod + dp3[i - 1][2] % mod) % mod;
        }
        return (dp3[n - 1][0] + dp3[n - 1][1] + dp3[n - 1][2]) % mod;
    }
};

标签:Attendance,dp3,dp2,Hard,552,return,day,dp,mod
From: https://www.cnblogs.com/zwyyy456/p/17489330.html

相关文章

  • 1483. Kth Ancestor of a Tree Node (Hard)
    Description1483.KthAncestorofaTreeNode(Hard)Youaregivenatreewithnnodesnumberedfrom0ton-1intheformofaparentarrayparentwhereparent[i]istheparentofithnode.Therootofthetreeisnode0.Findthekthancestorofagive......
  • 智能合约HardHat框架环境的搭建
    1.首先创建一个npm项目PSC:\Users\lcds\blockchainprojects>mkdirhardhatcontractPSC:\Users\lcds\blockchainprojects>cd.\hardhatcontract\2.运行 npminit-y  初始化项目PSC:\Users\lcds\blockchainprojects\hardhatcontract>npminit-yWrotetoC:\......
  • [ABC162E] Sum of gcd of Tuples (Hard)
    题面翻译给定\(n,k\),求\[\sum^k_{a_1=1}\sum^k_{a_2=1}\sum^k_{a_3=1}\dots\sum^k_{a_n=1}gcd(a_1,a_2,a_3,\dots,a_n)\mod\1000000007\]制約$2\\leq\N\\leq\10^5$$1\\leq\K\\leq\10^5$。思路点拨我们看到这么多\(\gcd\)的式子,我们自然想到莫比乌斯反演......
  • Doosan Excavator Inspection Diagnostic Tool DDT SCR DPF G2 Scan DCU ECU DMS-5 Ha
    DoosanExcavatorInspectionDiagnosticToolDDTSCRDPFG2ScanDCUECUDMS-5Hardware+Software2022.09Softwaredownloadlink:https://mega.nz/file/Bk8X1QxA#g49TrmFsIljfHQpAIkQlG-VIWSgug8kLq3VffqAW00YHardware+SoftwareVersionDoosanDDTSCRDoosan......
  • 2681. 英雄的力量 (Hard)
    问题描述2681.英雄的力量(Hard)给你一个下标从0开始的整数数组nums,它表示英雄的能力值。如果我们选出一部分英雄,这组英雄的力量定义为:i₀,i₁,...iₖ表示这组英雄在数组中的下标。那么这组英雄的力量为max(nums[i₀],nums[i₁]...nums[iₖ])²*min(nums[i₀]......
  • 41.缺失的第一个正数 (Hard)
    问题描述41.缺失的第一个正数(Hard)给你一个未排序的整数数组nums,请你找出其中没有出现的最小的正整数。请你实现时间复杂度为O(n)并且只使用常数级别额外空间的解决方案。示例1:输入:nums=[1,2,0]输出:3示例2:输入:nums=[3,4,-1,1]输出:2示例3:输入:nums=[......
  • 2646. 最小化旅行的价格总和 (Hard)
    问题描述2646.最小化旅行的价格总和(Hard)现有一棵无向、无根的树,树中有n个节点,按从0到n-1编号。给你一个整数n和一个长度为n-1的二维整数数组edges,其中edges[i]=[aᵢ,bᵢ]表示树中节点aᵢ和bᵢ之间存在一条边。每个节点都关联一个价格。给你一个......
  • send it failed() The virtual circuit was reset by the remote side executing a ha
    串口调试助手报错提示Thevirtualcircuitwasresetbytheremotesideexecutingahardorabortiveclose.forupdsocket,theremotehostwasunabletodeliverapreviouslysentUDPdategramandrespondedwithaportunreachableICMPpackettheapplicationsh......
  • 【每日一题】LeetCode 913.猫和老鼠(hard题)
    题目两位玩家分别扮演猫和老鼠,在一张无向图上进行游戏,两人轮流行动。图的形式是:graph[a]是一个列表,由满足ab是图中的一条边的所有节点b组成。老鼠从节点1开始,第一个出发;猫从节点2开始,第二个出发。在节点0处有一个洞。在每个玩家的行动中,他们必须沿着图中与所在当前......
  • CF1559D2 Mocha and Diana (Hard Version) 题解
    Luogu|Codeforces题意给定两个森林\(A\)和\(B\),均有编号\(1\)到\(n\)的节点,边数分别为\(m_1,m_2\)。现在进行加边操作,但是有两个要求:如果在第一个森林加一条\((u,v)\)的边,第二个森林也要进行同样的操作。反之同理。加边后两个森林依旧是森林。一棵树也是森林。......