Created
September 7, 2019 10:41
-
-
Save priyankvex/85956e12f1fbd337361b55dad53c4b7c to your computer and use it in GitHub Desktop.
Construct the array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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