Netcore Backend developer coding answer
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
def count_leading_zeros(num):
# Count leading zeros in a number
str_num = str(num)
count = 0
for digit in str_num:
if digit == ‘0’:
count += 1
else:
break
return count
def solve(n, tiles):
# Create DP array to store minimum leading zeros
dp = [[float(‘inf’)] * n for _ in range(n)]
# Create array to store products
products = [[1] * n for _ in range(n)]
# Initialize first cell
dp[0][0] = count_leading_zeros(tiles[0][0])
products[0][0] = tiles[0][0]
# Fill first row
for j in range(1, n):
products[0][j] = products[0][j-1] * tiles[0][j]
dp[0][j] = count_leading_zeros(products[0][j])
# Fill first column
for i in range(1, n):
products[i][0] = products[i-1][0] * tiles[i][0]
dp[i][0] = count_leading_zeros(products[i][0])
# Fill rest of the DP table
for i in range(1, n):
for j in range(1, n):
# Calculate product from top
top_product = products[i-1][j] * tiles[i][j]
top_zeros = count_leading_zeros(top_product)
# Calculate product from left
left_product = products[i][j-1] * tiles[i][j]
left_zeros = count_leading_zeros(left_product)
# Choose minimum leading zeros path
if top_zeros <= left_zeros:
dp[i][j] = top_zeros
products[i][j] = top_product
else:
dp[i][j] = left_zeros
products[i][j] = left_product
return dp[n-1][n-1]