在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ 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:
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. Note: The value of n won't exceed 100,000. 给定一个正整数 n,返回长度为 n 的所有可被视为可奖励的出勤记录的数量。 答案可能非常大,你只需返回结果mod 109 + 7的值。 学生出勤记录是只包含以下三个字符的字符串:
如果记录不包含多于一个'A'(缺勤)或超过两个连续的'L'(迟到),则该记录被视为可奖励的。 示例 1: 输入: n = 2 输出: 8 解释: 有8个长度为2的记录将被视为可奖励: "PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL" 只有"AA"不会被视为可奖励,因为缺勤次数超过一次。 注意:n 的值不会超过100000。 Runtime: 276 ms
Memory Usage: 19.7 MB
1 class Solution { 2 func checkRecord(_ n: Int) -> Int { 3 var M:Int = 1000000007; 4 var P:[Int] = [Int](repeating:0,count:n + 1) 5 var PorL:[Int] = [Int](repeating:0,count:n + 1) 6 P[0] = 1 7 PorL[0] = 1 8 PorL[1] = 2 9 for i in 1...n 10 { 11 P[i] = PorL[i - 1] 12 if i > 1 13 { 14 PorL[i] = (P[i] + P[i - 1] + P[i - 2]) % M 15 } 16 } 17 var res:Int = PorL[n]; 18 for i in 0..<n 19 { 20 var t:Int = (PorL[i] * PorL[n - 1 - i]) % M 21 res = (res + t) % M 22 } 23 return res 24 } 25 }
|
请发表评论