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):# Step 1: O(W) ## Four base cases (widths 0-3) for row wallsrow_combinations=[1,1,2,4]# Build row combinations up to current wall's widthwhilelen(row_combinations)<=m:row_combinations.append(sum(row_combinations[-4:]))# Compute total combinations # for constructing a wall of height N of varying widthstotal=[pow(c,n)forcinrow_combinations]# Step 3: O(W^2)# Find the number of unstable wall configurations # for a wall of height N of varying widthsunstable=[0,0]# Divide the wall into left part and right part,# and calculate the combination of left part and right part.# From width is 2, we start to consider about violationsforiinrange(2,m+1):# i: current total width, j: left width, i - j: right width# f: (left part - previous vertical violation)*right part# result = sum of f applied to all 1-if=lambdaj:(total[j]-unstable[j])*total[i-j]result=sum(map(f,range(1,i)))unstable.append(result)return(total[m]-unstable[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 →