Skip to content

Instantly share code, notes, and snippets.

@priyankvex
Created September 7, 2019 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save priyankvex/85956e12f1fbd337361b55dad53c4b7c to your computer and use it in GitHub Desktop.
Save priyankvex/85956e12f1fbd337361b55dad53c4b7c to your computer and use it in GitHub Desktop.
Construct the array
class Solution(object):
def solve(self, n, k, x):
a = [0] * n
b = [0] * n
if x == 1:
a[0] = 1
else:
b[0] = 1
for i in range(1, n):
a[i] = b[i-1]
b[i] = (a[i-1] * (k-1)) + (b[i-1] * (k-2))
return a[n-1]
if __name__ == "__main__":
n = 4
k = 3
x = 2
ans = Solution().solve(n, k, x)
print(ans)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment