Advertisement
Iam_Sandeep

Untitled

Apr 22nd, 2022
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. #User function Template for python3
  2.  
  3. class Solution:
  4. def countWays(self,n,k):
  5. dp=[[[-1]*(1+n) for i in range(k+1)] for j in range(4)]
  6. def find(posts,i,pc):
  7. if dp[posts][i][pc]!=-1:
  8. return dp[posts][i][pc]
  9. if posts==n-1:
  10. return 1
  11. ans=0
  12. if pc==1:
  13. for c in range(k):
  14. if c==i:
  15. ans+=find(posts+1,c,2)
  16. else:
  17. ans+=find(posts+1,c,1)
  18. elif pc==2:
  19. for c in range(k):
  20. if c!=i:
  21. ans+=find(posts+1,c,1)
  22. dp[posts][i][pc]=ans
  23. return ans
  24. ans=0
  25. for c in range(k):
  26. ans+=find(0,c,1)
  27. return int(ans%(1e9+7))
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement