<p>You have two types of tiles: a <code>2 x 1</code> domino shape and a tromino shape. You may rotate these shapes.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/15/lc-domino.jpg" style="width: 362px; height: 195px;" />
<p>Given an integer n, return <em>the number of ways to tile an</em> <code>2 x n</code> <em>board</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>In a tiling, every square must be covered by a tile. Two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the tilings has both squares occupied by a tile.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/15/lc-domino1.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The five different ways are show above.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 1000</code></li>
</ul>

def helper(i,j,n,nums,dp):

    if(i>=n):
        if(nums[n-1][0] == 1 and nums[n-1][0] and nums[n][0] == 0 and nums[n][1] == 0):
            return 1
        else:
            return 0

    if(dp[i][j]!=-1):
        return dp[i][j]

    if(j == 0):

        nums[i][0] = 1
        nums[i][1] = 1

        a = helper(i+1,0,n,nums,dp)

        nums[i][0] = 0
        nums[i][1] = 0

        nums[i][0] = 1
        nums[i][1] = 1
        nums[i+1][0] = 1
        nums[i+1][1] = 1

        b = helper(i+2,0,n,nums,dp)

        nums[i][0] = 0
        nums[i][1] = 0
        nums[i+1][0] = 0
        nums[i+1][1] = 0

        nums[i][0] = 1
        nums[i][1] = 1
        nums[i+1][0] = 1
        nums[i+1][1] = 0

        c = helper(i+1,1,n,nums,dp)

        nums[i][0] = 0
        nums[i][1] = 0
        nums[i+1][0] = 0
        nums[i+1][1] = 0

        nums[i][0] = 1
        nums[i][1] = 1
        nums[i+1][0] = 0
        nums[i+1][1] = 1

        d = helper(i+1,2,n,nums,dp)

        nums[i][0] = 0
        nums[i][1] = 0
        nums[i+1][0] = 0
        nums[i+1][1] = 0

        dp[i][j] = a+b+c+d
        return dp[i][j]

        # return a + b + c + d

    if(j == 1):
        nums[i][1] = 1
        nums[i+1][0] = 1
        nums[i+1][1] = 1

        a = helper(i+2,0,n,nums,dp)

        nums[i][1] = 0
        nums[i+1][0] = 0
        nums[i+1][1] = 0

        nums[i][1] = 1
        nums[i+1][1] = 1

        b = helper(i+1,2,n,nums,dp)

        nums[i][1] = 0
        nums[i+1][1] = 0

        dp[i][j] = a+b
        return dp[i][j]

        # return a + b

    if(j == 2):
        nums[i][0] = 1
        nums[i+1][0] = 1
        nums[i+1][1] = 1

        a = helper(i+2,0,n,nums,dp)

        nums[i][0] = 0
        nums[i+1][0] = 0
        nums[i+1][1] = 0

        nums[i][0] = 1
        nums[i+1][0] = 1
        b= helper(i+1,1,n,nums,dp)
        nums[i][0] = 0
        nums[i+1][0] = 0

        dp[i][j] = a+b
        return dp[i][j]

        # return a + b




class Solution:
    def numTilings(self, n: int) -> int:
        tiles = [[0,0] for _ in range(n+1)]
        dp = [[-1] * 3 for _ in range(n+1)]
        return helper(0,0,n,tiles,dp) % (10**9 + 7)






17300