Number of ways to paint 3 X N grid with 3 colors

https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/

看上面的解法就能推导出来,code部分其实简单。就是对于三种color来说,每一个column只有两种pattern

(1) 121

(2) 123

class Solution:
    def numOfWays(self, n: int) -> int:
        a121 = 6
        a123 = 6
        mod = 10**9+7
        for _ in range(n-1):
            a121,a123 = a123*2 + 3* a121, a121*2 + 2*a123
            a121 = a121 % mod
            a123 = a123 % mod
        return (a121+a123) % mod
        

Last updated

Was this helpful?