Advertisement
rishu110067

Untitled

Feb 7th, 2022
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.68 KB | None | 0 0
  1. public class Solution {
  2.     public int NumTilings(int n) {
  3.        
  4.         if(n == 1) return 1;
  5.        
  6.         int[] f = new int[n+1];
  7.         int[] l = new int[n+1];
  8.         int[] u = new int[n+1];
  9.        
  10.         f[1]=1;
  11.         f[2]=2;
  12.         l[1]=0;
  13.         l[2]=1;
  14.        
  15.         u[1]=0;
  16.         u[2]=1;
  17.        
  18.         int M = 1000000007;
  19.         for(int index = 3; index <= n; index++)
  20.         {
  21.             f[index] = ((f[index-1] + f[index-2]) % M + (l[index-1] + u[index-1]) % M) % M;
  22.             l[index] = (f[index-2] + u[index-1]) % M;
  23.             u[index] = (f[index-2] + l[index-1]) % M;
  24.         }
  25.        
  26.        
  27.         return f[n];
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement