Участник:UlitinAleksander/student-attendance

Материал из DISCOPAL
Перейти к: навигация, поиск

python3

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

class Solution(object):
   def checkRecord(self, n: int) -> int:
        dp, newdp, i = {}, {}, 1
        dp[(1,2)] = 1
        dp[(1,0)], dp[(1,1)], dp[(0,2)] = 2, 2, 2
        dp[(0,0)], dp[(0,1)] = 3, 3
 
        while i < n:
            newdp[(1,2)] = dp[(1,0)]
            newdp[(1,0)] = dp[(1,0)]+dp[(1,1)]
            newdp[(1,1)] = dp[(1,0)]+dp[(1,2)]
            newdp[(0,2)] = dp[(0,0)]+dp[(1,0)]
            newdp[(0,0)] = newdp[(0,2)]+dp[(0,1)]
            newdp[(0,1)] = newdp[(0,2)]+dp[(0,2)]
            i += 1
            dp = newdp
            newdp = {}
 
        return dp[(0,0)]%(1000000007)