human
Count of N | Function to return the count of such numbers.
For 1 - digit numbers , the count is 10 irrespective of K.
dp [ j ] stores the number of such i - digit numbers ending with j.
Stores the results of length i.
Initialize count for 1 - digit numbers.
Compute values for count of digits greater than 1.
Find the range of allowed numbers if last digit is j.
Perform Range update.
Prefix sum to find actual count of i - digit numbers ending with j.
Update dp [ ].
Stores the final answer.
Return the final answer.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to count of N and the other steps required.
 def getCount ( n , K ) :
	if ( n == 1 ) :
		return 10
	dp = [ 0 ] * 11
	next = [ 0 ] * 11
	for i in range ( 1 , 9 + 1 ) :
		dp [ i ] = 1
	for i in range ( 2 , n + 1 ) :
		for j in range ( 9 + 1 ) :
			l = max ( 0 , j - k )
			r = min ( 9 , j + k )
			next [ l ] += dp [ j ]
			next [ r + 1 ] -= dp [ j ]
		for j in range ( 1 , 9 + 1 ) :
			next [ j ] += next [ j - 1 ]
		for j in range ( 10 ) :
			dp [ j ] = next [ j ]
			next [ j ] = 0
	count = 0
	for i in range ( 9 + 1 ) :
		count += dp [ i ]
	return count
if __name__ == ' _ _ main _ _ ' :
	n = 2
	k = 1
	print ( getCount ( n , k ) )