[英]Explain the logic of 'Count of N-digit numbers with absolute difference of adjacent digits not exceeding K' in Detail
Can someone please help me understand the logic of following Dynamic Programmming question Found this one at geeksforgeeks.com.有人可以帮我理解以下动态编程问题的逻辑在 geeksforgeeks.com 找到这个问题。 I am unable to understand even after going through the answer provided.
即使经过提供的答案,我也无法理解。
Question:问题:
Count of N-digit numbers with absolute difference of adjacent digits not exceeding K |
相邻位数绝对差不超过K的N位数个数| Set 2
设置 2
Given two integers N and K, the task is to find the count of N-digit numbers such that the absolute difference of adjacent digits in the number is not greater than K.
给定两个整数 N 和 K,任务是找到 N 位数字的计数,使得该数字中相邻数字的绝对差不大于 K。
Examples:
例子:
Input: N = 2, K = 1
输入:N = 2,K = 1
Output: 26
Output:26
Explanation: The numbers are 10, 11, 12, 21, 22, 23, 32, 33, 34, 43, 44, 45, 54, 55, 56, 65, 66, 67, 76, 77, 78, 87, 88, 89, 98, 99
解释: 数字是 10, 11, 12, 21, 22, 23, 32, 33, 34, 43, 44, 45, 54, 55, 56, 65, 66, 67, 76, 77, 78, 87, 88 , 89, 98, 99
Input: N = 3, K = 2
输入:N = 3,K = 2
Output: 188
Output:188
Python3 solution: Python3解决方案:
# Function to return the count of such numbers
def getCount(n, K):
# For 1-digit numbers, the count is 10 irrespective of K
if(n == 1):
return 10
# dp[j] stores the number of such i-digit numbers ending with j
dp = [0] * 11
# Stores the results of length i
next = [0] * 11
# Initialize count for 1-digit numbers
for i in range(1, 9 + 1):
dp[i] = 1
# Compute values for count of digits greater than 1
for i in range(2, n + 1):
for j in range(9 + 1):
# Find the range of allowed numbers if last digit is j
l = max(0, j - k)
r = min(9, j + k)
# Perform Range update
next[l] += dp[j]
next[r + 1] -= dp[j]
# Prefix sum to find actual count of i-digit numbers ending with j
for j in range(1, 9 + 1):
next[j] += next[j - 1]
# Update dp[]
for j in range(10):
dp[j] = next[j]
next[j] = 0
# Stores the final answer
count = 0
for i in range(9 + 1):
count += dp[i]
return count
if __name__ == '__main__':
n = 2
k = 1
print(getCount(n, k))
This code is contributed by Shivam Singh此代码由 Shivam Singh 提供
Link: https://www.google.com/amp/s/www.geeksforgeeks.org/count-of-n-digit-numbers-with-absolute-difference-of-adjacent-digits-not-exceeding-k-set-2/amp/链接: https://www.google.com/amp/s/www.geeksforgeeks.org/count-of-n-digit-numbers-with-absolute-difference-of-adjacent-digits-not-exceeding-k-设置 2/amp/
I am unable to understand the logic in following lines我无法理解以下几行的逻辑
# Compute values for count of
# digits greater than 1
for i in range(2, n + 1):
for j in range(9 + 1):
# Find the range of allowed
# numbers if last digit is j
l = max(0, j - k)
r = min(9, j + k)
# Perform Range update
next[l] += dp[j]
next[r + 1] -= dp[j]
# Prefix sum to find actual count
# of i-digit numbers ending with j
for j in range(1, 9 + 1):
next[j] += next[j - 1]
# Update dp[]
for j in range(10):
dp[j] = next[j]
next[j] = 0
N=2
so we are dealing with strictly 2 digit numbers N=2
所以我们正在处理严格的 2 位数字
K=1
so each digit in the current number shall be no more than 1 digit greater or less than its neighbors K=1
,因此当前数字中的每个数字应比其相邻数字大或小不超过 1 个数字
While the answer is 26
(the count of such numbers), lets take a look at why the answer includes 10
, 11
, and 12
but not 13
虽然答案是
26
(这些数字的计数),但让我们看看为什么答案包括10
、 11
和12
而不是13
10
is good as the absolute value of 1
- 0
(the digits of 10
) is less than or equal to 1
(the value of K
) 10
是好的,因为1
- 0
( 10
的数字)的绝对值小于或等于1
( K
的值)
13
is bad as the absolute value of 1
- 3
is 2
and 2
is greater than K
13
不好,因为1
- 3
的绝对值为2
且2
大于K
Note that as N
increases, the number of pairwise digit comparisons required will also increase.请注意,随着
N
的增加,所需的成对数字比较的数量也会增加。
I'll skip parsing someone else's code with meaningless variable names.我将跳过用无意义的变量名解析别人的代码。 Here is how I might attempt to solve this:
以下是我可能尝试解决的方法:
def is_valid_number(number_to_test, allowed_digit_spread):
if number_to_test < 10:
# apparently all 1 digit numbers pass
return True
# get the individual digits as a list
digits = [int(digit) for digit in str(number_to_test)]
for i in range(1, len(digits)):
current_digit = digits[i]
prior_digit = digits[i-1]
if abs(current_digit - prior_digit) > allowed_digit_spread:
# bad pairwise compare so reject
return False
return True
def get_numbers_to_test(allowed_digits):
start = pow(10, allowed_digits -1)
end = pow(10, allowed_digits)
return range(start, end)
def getCount(allowed_digits, allowed_digit_spread):
count = 0
for n in get_numbers_to_test(allowed_digits):
if is_valid_number(n, allowed_digit_spread):
count += 1
return count
if __name__ == '__main__':
n = 2
k = 1
print(getCount(n, k))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.