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.
NUM=10**9+7defcountArray(n,k,x):dp=[[0for_inrange(2)]for_inrange(n+1)]# dp[length][ends in x]dp[2][0]=k-2ifx!=1elsek-1dp[2][1]=1ifx!=1else0foriinrange(3,n+1):dp[i][0]=((((k-2)%NUM)*(dp[i-1][0]%NUM))%NUM+(((k-1)%NUM)*(dp[i-1][1]%NUM))%NUM)%NUMdp[i][1]=dp[i-1][0]%NUMreturndp[n][1]%NUM
My initial solution - which consumed too much memory - involved computing the number of arrays that could be constructed that concluded with a specific number l but, at its limit it creates a memoisation array of size 4 x 10¹⁰ bytes which is... a bit too much RAM usage. However, ultimately all that matters is whether or not it ended with x or not.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Construct the Array
You are viewing a single comment's thread. Return to all comments →
My initial solution - which consumed too much memory - involved computing the number of arrays that could be constructed that concluded with a specific number l but, at its limit it creates a memoisation array of size 4 x 10¹⁰ bytes which is... a bit too much RAM usage. However, ultimately all that matters is whether or not it ended with x or not.