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.
Having some difficult with a valid JavaScript version.
The values that are failing are when combinations get over MOD (10^9 +7)
Has anybody got a JavaScript version working or can spot the error in this code? Thanks
*(legoBlocks(8,10) on my code is giving 634608018.
Hacker rank is giving 634597424*
functionlegoBlocks(h,w){// Write your code hereletmod=(Math.pow(10,9)+7)//different combinations for a width when height is 1lett=[0];if(w>=1)t.push(1)if(w>=2)t.push(2)if(w>=3)t.push(4)if(w>=4)t.push(8)while(w>=5&&t.length<=w){letstart=t.length-4letarr=t.slice(start,t.length)letsum=arr.reduce((partialSum,a)=>partialSum+a,0)t.push(sum)}//different combinations for width given height h//e.g. total[height=2] is t^2, total[height=3] is t^3lettotal=t.slice()for(letouter=2;outer<=h;outer++){for(leti=0;i<=w;i++){total[i]=(t[i]*total[i])}}letsolid=[]solid.length=w+1;solid.fill(0)solid[1]=1//s[x] = total[x] - (SUM (where i starts at 1 and ends at x-1) s[i] * total[w-i]))if(w>1){for(letww=2;ww<=w;ww++){letunsolid_sum=0;for(leti=1;i<ww;i++){unsolid_sum+=(((solid[i])*(total[ww-i])))}solid[ww]=((total[ww]-unsolid_sum))}}
return solid[w] % mod
}
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 →
Having some difficult with a valid JavaScript version. The values that are failing are when combinations get over MOD (10^9 +7) Has anybody got a JavaScript version working or can spot the error in this code? Thanks
*(legoBlocks(8,10) on my code is giving 634608018.
Hacker rank is giving 634597424*
}