We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
deflegoBlocks(n,m):# If only one column or one row, there's only one way to build the wallifm==1:return1ifn==1:return1if1<=m<=4else0MOD=pow(10,9)+7# Number of layouts to build a 1 x k wall for k = 1 to mp=[0,1,2,4,8]#Basecasesforwidths1to4forkinrange(5,m+1):# For width k, sum the previous four widthsp.append(sum(p[-i]%MODforiinrange(1,5)))# Number of layouts to build an n x k wall for k = 1 to mp=[pow(val,n,MOD)forvalinp]# Number of good layouts (without any complete row gaps) to build n x k wall for k = 1 to mg=[0,1]forkinrange(2,m+1):# Calculate the number of bad layouts for width kbad_layouts_num=sum(g[j]*p[k-j]forjinrange(1,k))%MOD# Explanation:# g[j] * p[k - j] represents the number of ways to build an n x k wall with a vertical division# at position j, resulting in a good layout on the left (g[j]) and any layout on the right (p[k - j]).# Summing these products for all j from 1 to k-1 gives the total number of bad layouts where there# is at least one complete vertical division in the wall.# Subtract bad layouts from total layouts to get good layoutsg.append((p[k]-bad_layouts_num)%MOD)returng[m]
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Lego Blocks
You are viewing a single comment's thread. Return to all comments →