Matrix A, consisting of N rows and N columns of non-negative integers, is given. Rows are numbered from 0 to N−1 (from top to bottom). Columns are numbered from 0 to N−1 (from left to right). We would like to find a path that starts at the upper-left corner (0, 0) and, moving only right and down, finishes at the bottom-right corner (N−1, N−1). Then, all the numbers on this path will be multiplied together.
Find a path such that the product of all the numbers on the path contain the minimal number of trailing zeros. We assume that 0 has 1 trailing zero.
Write a function:
def solution(A)
that, given matrix A, returns the minimal number of trailing zeros.
Examples:
1. Given matrix A below:
the function should return 1. The optimal path is: (0,0) → (0,1) → (0,2) → (1,2) → (2,2) → (2,3) → (3,3). The product of numbers 2, 10, 1, 4, 2, 1, 1 is 160, which has one trailing zero. There is no path that yields a product with no trailing zeros.
2. Given matrix A below:
the function should return 2. One of the optimal paths is: (0,0) → (1,0) → (1,1) → (1,2) → (2,2) → (3,2) → (3,3). The product of numbers 10, 1, 1, 1, 10, 1, 1 is 100, which has two trailing zeros. There is no path that yields a product with fewer than two trailing zeros.
3. Given matrix A below:
the function should return 1. One of the optimal paths is: (0,0) → (0,1) → (1,1) → (1,2) → (2,2). The product of numbers 10, 10, 0, 10, 10 is 0, which has one trailing zero. There is no path that yields a product with no trailing zeros.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..500];
- each element of matrix A is an integer within the range [0..1,000,000,000].
#minimizing numbers and counting zeros
def change_number(n):
if n==0: return([0,1])
zeros=0
if (n%10)==0:
zeros=zeros_amount(n)
zero=int('1'+zeros*'0')
n=n/zero
if (n%5)==0: n=5
elif (n%2)==0: n=2
else: n=1
return ([n,zeros])
#counting amount of zeros in a number
def zeros_amount(n):
return (len(str(n)) - len(str(n).rstrip('0')))
#finding minimum number with minimum amount of zeroes
def find_min(n,m):
if m[0]==n[0]==0: return ([0,1])
if m[0]==0 or n[1]<m[1]: return(n)
elif n[0]==0 or m[1]<n[1]: return (m)
#elif n[1]<m[1]: return (n)
#elif m[1]<n[1]: return (m)
elif n[0]<m[0]: return (n)
else: return(m)
#multiple two array elements
def mult(a,b):
if a[0]==0 or b[0]==0: return([0,1])
first,second =a[0]*b[0], a[1]+b[1]
if first%10==0:
first=first//10
second+=1
return [first,second]
def solution(A):
N=len(A)
zero_included=0
#creating 1st line
firstline=A[0]
firstline[0]=change_number(firstline[0])
for x in range(1,N): firstline[x] = mult(firstline[x-1], change_number(firstline[x]))
m=1
while m<N:
secondline=A[m]
newline=[]
for x in range(0,N):
secondline[x]=change_number(secondline[x])
if (x==0): newline.append(mult(firstline[0],secondline[0]))
elif (secondline[x][0]==0):
zero_included=1
newline.append([0,1])
else:
newline.append(find_min(mult(firstline[x],secondline[x]),mult(secondline[x],newline[-1])))
firstline=newline
m +=1
result=firstline[-1][1]
if zero_included!=0: result=min(result,1)
return(result)
#minimizing numbers and counting zeros
def change_number(n):
if n==0: return([0,1])
zeros=0
if (n%10)==0:
zeros=zeros_amount(n)
zero=int('1'+zeros*'0')
n=n/zero
if (n%5)==0: n=5
elif (n%2)==0: n=2
else: n=1
return ([n,zeros])
#counting amount of zeros in a number
def zeros_amount(n):
return (len(str(n)) - len(str(n).rstrip('0')))
#finding minimum number with minimum amount of zeroes
def find_min(n,m):
if m[0]==n[0]==0: return ([0,1])
if m[0]==0 or n[1]<m[1]: return(n)
elif n[0]==0 or m[1]<n[1]: return (m)
elif n[0]<m[0]: return (n)
else: return(m)
#multiple two array elements
def mult(a,b):
if a[0]==0 or b[0]==0: return([0,1])
first,second =a[0]*b[0], a[1]+b[1]
if first%10==0:
first=first//10
second+=1
return [first,second]
def solution(A):
N=len(A)
zero_included=0
#creating 1st line
firstline=A[0]
firstline[0]=change_number(firstline[0])
for x in range(1,N): firstline[x] = mult(firstline[x-1], change_number(firstline[x]))
m=1
while m<N:
secondline=A[m]
newline=[]
for x in range(0,N):
secondline[x]=change_number(secondline[x])
if (x==0): newline.append(mult(firstline[0],secondline[0]))
elif (secondline[x][0]==0):
zero_included=1
newline.append([0,1])
else:
newline.append(find_min(mult(firstline[x],secondline[x]),mult(secondline[x],newline[-1])))
firstline=newline
m +=1
result=firstline[-1][1]
if zero_included!=0: result=min(result,1)
return(result)
#minimizing numbers and counting zeros
def change_number(n):
if n==0: return([0,1])
zeros=0
if (n%10)==0:
zeros=zeros_amount(n)
zero=int('1'+zeros*'0')
n=n/zero
if (n%5)==0: n=5
elif (n%2)==0: n=2
else: n=1
return ([n,zeros])
#counting amount of zeros in a number
def zeros_amount(n):
return (len(str(n)) - len(str(n).rstrip('0')))
#finding minimum number with minimum amount of zeroes
def find_min(n,m):
if m[0]==n[0]==0: return ([0,1])
if m[0]==0 or n[1]<m[1]: return(n)
elif n[0]==0 or m[1]<n[1]: return (m)
elif n[0]<m[0]: return (n)
else: return(m)
#multiple two array elements
def mult(a,b):
if a[0]==0 or b[0]==0: return([0,1])
first,second =a[0]*b[0], a[1]+b[1]
if first%10==0:
first=first//10
second+=1
return [first,second]
def solution(A):
N=len(A)
zero_included=0
#creating 1st line
firstline=A[0]
firstline[0]=change_number(firstline[0])
for x in range(1,N): firstline[x] = mult(firstline[x-1], change_number(firstline[x]))
m=1
while m<N:
secondline=A[m]
newline=[]
for x in range(0,N):
secondline[x]=change_number(secondline[x])
if (x==0): newline.append(mult(firstline[0],secondline[0]))
elif (secondline[x][0]==0):
zero_included=1
newline.append([0,1])
else:
newline.append(find_min(mult(firstline[x],secondline[x]),mult(secondline[x],newline[-1])))
firstline=newline
m +=1
result=firstline[-1][1]
if zero_included!=0: result=min(result,1)
return(result)
#minimizing numbers and counting zeros
def change_number(n):
if n==0: return([0,1])
zeros=0
if (n%10)==0:
zeros=zeros_amount(n)
zero=int('1'+zeros*'0')
n=n/zero
if (n%5)==0: n=5
elif (n%2)==0: n=2
else: n=1
return ([n,zeros])
#counting amount of zeros in a number
def zeros_amount(n):
return (len(str(n)) - len(str(n).rstrip('0')))
#finding minimum number with minimum amount of zeroes
def find_min(n,m):
if m[0]==n[0]==0: return ([0,1])
if m[0]==0 or n[1]<m[1]: return(n)
elif n[0]==0 or m[1]<n[1]: return (m)
elif n[0]<m[0]: return (n)
else: return(m)
#multiple two array elements
def mult(a,b):
if a[0]==0 or b[0]==0: return([0,1])
first,second= a[0]*b[0], a[1]+b[1]
if first%10==0:
first=first//10
second+=1
return [first,second]
def solution(A):
N=len(A)
zero_included=0
#creating 1st line
firstline=A[0]
firstline[0]=change_number(firstline[0])
for x in range(1,N): firstline[x] = mult(firstline[x-1], change_number(firstline[x]))
m=1
while m<N:
secondline=A[m]
newline=[]
for x in range(0,N):
secondline[x]=change_number(secondline[x])
if (x==0): newline.append(mult(firstline[0],secondline[0]))
elif (secondline[x][0]==0):
zero_included=1
newline.append([0,1])
else:
newline.append(find_min(mult(firstline[x],secondline[x]),mult(secondline[x],newline[-1])))
firstline=newline
m +=1
result=firstline[-1][1]
if zero_included!=0: result=min(result,1)
return(result)
#minimizing numbers and counting zeros
def change_number(n):
if n==0: return([0,1])
zeros=0
if (n%10)==0:
zeros=zeros_amount(n)
zero=int('1'+zeros*'0')
n=n/zero
if (n%5)==0: n=5
elif (n%2)==0: n=2
else: n=1
return ([n,zeros])
#counting amount of zeros in a number
def zeros_amount(n):
return (len(str(n)) - len(str(n).rstrip('0')))
#finding smaller number with minimum amount of zeroes
def find_min(n,m):
if m[0]==n[0]==0: return ([0,1])
if m[0]==0 or n[1]<m[1]: return(n)
elif n[0]==0 or m[1]<n[1]: return (m)
elif n[0]<m[0]: return (n)
else: return(m)
#multiple two array elements
def mult(a,b):
if a[0]==0 or b[0]==0: return([0,1])
first,second= a[0]*b[0], a[1]+b[1]
if first%10==0:
first=first//10
second+=1
return [first,second]
def solution(A):
N=len(A)
zero_included=0
#creating 1st line
firstline=A[0]
firstline[0]=change_number(firstline[0])
for x in range(1,N): firstline[x] = mult(firstline[x-1], change_number(firstline[x]))
m=1
while m<N:
secondline=A[m]
newline=[]
for x in range(0,N):
secondline[x]=change_number(secondline[x])
if (x==0): newline.append(mult(firstline[0],secondline[0]))
elif (secondline[x][0]==0):
zero_included=1
newline.append([0,1])
else:
newline.append(find_min(mult(firstline[x],secondline[x]),mult(secondline[x],newline[-1])))
firstline=newline
m +=1
result=firstline[-1][1]
if zero_included!=0: result=min(result,1)
return(result)
#minimizing numbers and counting zeros
def change_number(n):
if n==0: return([0,1])
zeros=0
if (n%10)==0:
zeros=zeros_amount(n)
zero=int('1'+zeros*'0')
n=n/zero
if (n%5)==0: n=5
elif (n%2)==0: n=2
else: n=1
return ([n,zeros])
#counting amount of zeros in a number
def zeros_amount(n):
return (len(str(n)) - len(str(n).rstrip('0')))
#finding smaller number with minimum amount of zeroes
def find_min(n,m):
if m[0]==n[0]==0: return ([0,1])
if m[0]==0 or n[1]<m[1]: return(n)
elif n[0]==0 or m[1]<n[1]: return (m)
elif n[0]<m[0]: return (n)
else: return(m)
#multiple two array elements
def mult(a,b):
if a[0]==0 or b[0]==0: return([0,1])
first,second= a[0]*b[0], a[1]+b[1]
if first%10==0:
first=first//10
second+=1
return [first,second]
def solution(A):
N=len(A)
zero_included=0
#creating first 1st line(it is different from other lines
firstline=A[0]
firstline[0]=change_number(firstline[0])
for x in range(1,N): firstline[x] = mult(firstline[x-1], change_number(firstline[x]))
m=1
while m<N:
secondline=A[m]
newline=[]
for x in range(0,N):
secondline[x]=change_number(secondline[x])
if (x==0): newline.append(mult(firstline[0],secondline[0]))
elif (secondline[x][0]==0):
zero_included=1
newline.append([0,1])
else:
newline.append(find_min(mult(firstline[x],secondline[x]),mult(secondline[x],newline[-1])))
firstline=newline
m +=1
result=firstline[-1][1]
if zero_included!=0: result=min(result,1)
return(result)
#minimizing numbers and counting zeros
def change_number(n):
if n==0: return([0,1])
zeros=0
if (n%10)==0:
zeros=zeros_amount(n)
zero=int('1'+zeros*'0')
n=n/zero
if (n%5)==0: n=5
elif (n%2)==0: n=2
else: n=1
return ([n,zeros])
#counting amount of zeros in a number
def zeros_amount(n):
return (len(str(n)) - len(str(n).rstrip('0')))
#finding smaller number with minimum amount of zeroes
def find_min(n,m):
if m[0]==n[0]==0: return ([0,1])
if m[0]==0 or n[1]<m[1]: return(n)
elif n[0]==0 or m[1]<n[1]: return (m)
elif n[0]<m[0]: return (n)
else: return(m)
#multiple two array elements
def mult(a,b):
if a[0]==0 or b[0]==0: return([0,1])
first,second= a[0]*b[0], a[1]+b[1]
if first%10==0:
first=first//10
second+=1
return [first,second]
def solution(A):
N=len(A)
zero_included=0
#creating first 1st line(it is different from other lines
firstline=A[0]
firstline[0]=change_number(firstline[0])
for x in range(1,N): firstline[x] = mult(firstline[x-1], change_number(firstline[x]))
m=1
while m<N:
secondline=A[m]
newline=[]
for x in range(0,N):
secondline[x]=change_number(secondline[x])
if (x==0): newline.append(mult(firstline[0],secondline[0]))
elif (secondline[x][0]==0):
zero_included=1
newline.append([0,1])
else:
newline.append(find_min(mult(firstline[x],secondline[x]),mult(secondline[x],newline[-1])))
firstline=newline
m +=1
result=firstline[-1][1]
if zero_included!=0: result=min(result,1)
return(result)
#minimizing numbers and counting zeros
def change_number(n):
if n==0: return([0,1])
zeros=0
if (n%10)==0:
zeros=zeros_amount(n)
zero=int('1'+zeros*'0')
n=n/zero
if (n%5)==0: n=5
elif (n%2)==0: n=2
else: n=1
return ([n,zeros])
#counting amount of zeros in a number
def zeros_amount(n):
return (len(str(n)) - len(str(n).rstrip('0')))
#finding smaller number with minimum amount of zeroes
def find_min(n,m):
if m[0]==n[0]==0: return ([0,1])
if m[0]==0 or n[1]<m[1]: return(n)
elif n[0]==0 or m[1]<n[1]: return (m)
elif n[0]<m[0]: return (n)
else: return(m)
#multiple two array elements
def mult(a,b):
if a[0]==0 or b[0]==0: return([0,1])
first,second= a[0]*b[0], a[1]+b[1]
if first%10==0:
first=first//10
second+=1
return [first,second]
def solution(A):
N=len(A)
zero_included=0
#creating first 1st line(it is different from other lines
firstline=A[0]
firstline[0]=change_number(firstline[0])
for x in range(1,N): firstline[x] = mult(firstline[x-1], change_number(firstline[x]))
m=1
while m<N:
secondline=A[m]
newline=[]
for x in range(0,N):
secondline[x]=change_number(secondline[x])
if (x==0): newline.append(mult(firstline[0],secondline[0]))
elif (secondline[x][0]==0):
zero_included=1
newline.append([0,1])
else:
newline.append(find_min(mult(firstline[x],secondline[x]),mult(secondline[x],newline[-1])))
firstline=newline
m +=1
#finding result(last number in array), and comparing it to 1 if there was there was 0 element somewhere
result=firstline[-1][1]
if zero_included!=0: result=min(result,1)
return(result)
#minimizing numbers and counting zeros
def change_number(n):
if n==0: return([0,1])
zeros=0
if (n%10)==0:
zeros=zeros_amount(n)
zero=int('1'+zeros*'0')
n=n/zero
if (n%5)==0: n=5
elif (n%2)==0: n=2
else: n=1
return ([n,zeros])
#counting amount of zeros in a number
def zeros_amount(n):
return (len(str(n)) - len(str(n).rstrip('0')))
#finding smaller number with minimum amount of zeroes
def find_min(n,m):
if m[0]==n[0]==0: return ([0,1])
if m[0]==0 or n[1]<m[1]: return(n)
elif n[0]==0 or m[1]<n[1]: return (m)
elif n[0]<m[0]: return (n)
else: return(m)
#multiple two array elements
def mult(a,b):
if a[0]==0 or b[0]==0: return([0,1])
first,second= a[0]*b[0], a[1]+b[1]
if first%10==0:
first=first//10
second+=1
return [first,second]
def solution(A):
N=len(A)
zero_included=0
#creating first 1st line(it is different from other lines
firstline=A[0]
firstline[0]=change_number(firstline[0])
for x in range(1,N): firstline[x] = mult(firstline[x-1], change_number(firstline[x]))
m=1
while m<N:
secondline=A[m]
newline=[]
for x in range(0,N):
secondline[x]=change_number(secondline[x])
if (x==0): newline.append(mult(firstline[0],secondline[0]))
elif (secondline[x][0]==0):
zero_included=1
newline.append([0,1])
else:
newline.append(find_min(mult(firstline[x],secondline[x]),mult(secondline[x],newline[-1])))
firstline=newline
m +=1
#finding result(zeros in last element of array), and comparing it to 1 if there was there was 0 element somewhere
result=firstline[-1][1]
if zero_included!=0: result=min(result,1)
return(result)
#minimizing numbers and counting zeros
def change_number(n):
if n==0: return([0,1])
zeros=0
if (n%10)==0:
zeros=zeros_amount(n)
zero=int('1'+zeros*'0')
n=n/zero
if (n%5)==0: n=5
elif (n%2)==0: n=2
else: n=1
return ([n,zeros])
#counting amount of zeros in a number
def zeros_amount(n):
return (len(str(n)) - len(str(n).rstrip('0')))
#finding smaller number with minimum amount of zeroes
def find_min(n,m):
if m[0]==n[0]==0: return ([0,1])
if m[0]==0 or n[1]<m[1]: return(n)
elif n[0]==0 or m[1]<n[1]: return (m)
elif n[0]<m[0]: return (n)
else: return(m)
#multiple two array elements
def mult(a,b):
if a[0]==0 or b[0]==0: return([0,1])
first,second= a[0]*b[0], a[1]+b[1]
if first%10==0:
first=first//10
second+=1
return [first,second]
def solution(A):
N=len(A)
zero_included=0
#creating first 1st line(it is different from other lines
firstline=A[0]
firstline[0]=change_number(firstline[0])
for x in range(1,N): firstline[x] = mult(firstline[x-1], change_number(firstline[x]))
m=1
while m<N:
secondline=A[m]
newline=[]
for x in range(0,N):
secondline[x]=change_number(secondline[x])
if (x==0): newline.append(mult(firstline[0],secondline[0]))
elif (secondline[x][0]==0):
zero_included=1
newline.append([0,1])
else:
newline.append(find_min(mult(firstline[x],secondline[x]),mult(secondline[x],newline[-1])))
firstline=newline
m +=1
#finding result(zeros in last element of array), and comparing it to 1 if there was there was 0 element somewhere
result=firstline[-1][1]
if zero_included!=0: result=min(result,1)
return(result)
#minimizing numbers and counting zeros
def change_number(n):
if n==0: return([0,1])
zeros=0
if (n%10)==0:
zeros=zeros_amount(n)
zero=int('1'+zeros*'0')
n=n/zero
if (n%5)==0: n=5
elif (n%2)==0: n=2
else: n=1
return ([n,zeros])
#counting amount of zeros in a number
def zeros_amount(n):
return (len(str(n)) - len(str(n).rstrip('0')))
#finding smaller number with minimum amount of zeroes
def find_min(n,m):
if m[0]==n[0]==0: return ([0,1])
if m[0]==0 or n[1]<m[1]: return(n)
elif n[0]==0 or m[1]<n[1]: return (m)
elif n[0]<m[0]: return (n)
else: return(m)
#multiple two array elements
def mult(a,b):
if a[0]==0 or b[0]==0: return([0,1])
first,second= a[0]*b[0], a[1]+b[1]
if first%10==0:
first=first//10
second+=1
return [first,second]
def solution(A):
N=len(A)
zero_included=0
#creating first 1st line(it is different from other lines
firstline=A[0]
firstline[0]=change_number(firstline[0])
for x in range(1,N): firstline[x] = mult(firstline[x-1], change_number(firstline[x]))
m=1
while m<N:
secondline=A[m]
newline=[]
for x in range(0,N):
secondline[x]=change_number(secondline[x])
if (x==0): newline.append(mult(firstline[0],secondline[0]))
elif (secondline[x][0]==0):
zero_included=1
newline.append([0,1])
else:
newline.append(find_min(mult(firstline[x],secondline[x]),mult(secondline[x],newline[-1])))
firstline=newline
m +=1
#finding result(zeros in last element of array), and comparing it to 1 if there was there was 0 element somewhere
result=firstline[-1][1]
if zero_included!=0: result=min(result,1)
return(result)
#minimizing numbers and counting zeros
def change_number(n):
if n==0: return([0,1])
zeros=0
if (n%10)==0:
zeros=zeros_amount(n)
zero=int('1'+zeros*'0')
n=n/zero
if (n%5)==0: n=5
elif (n%2)==0: n=2
else: n=1
return ([n,zeros])
#counting amount of zeros in a number
def zeros_amount(n):
return (len(str(n)) - len(str(n).rstrip('0')))
#finding smaller number with minimum amount of zeroes
def find_min(n,m):
if m[0]==n[0]==0: return ([0,1])
if m[0]==0 or n[1]<m[1]: return(n)
elif n[0]==0 or m[1]<n[1]: return (m)
elif n[0]<m[0]: return (n)
else: return(m)
#multiple two array elements
def mult(a,b):
if a[0]==0 or b[0]==0: return([0,1])
first,second= a[0]*b[0], a[1]+b[1]
if first%10==0:
first=first//10
second+=1
return [first,second]
def solution(A):
N=len(A)
zero_included=0
#creating first 1st line(it is different from other lines
firstline=A[0]
firstline[0]=change_number(firstline[0])
for x in range(1,N): firstline[x] = mult(firstline[x-1], change_number(firstline[x]))
m=1
while m<N:
secondline=A[m]
newline=[]
for x in range(0,N):
secondline[x]=change_number(secondline[x])
if (x==0): newline.append(mult(firstline[0],secondline[0]))
elif (secondline[x][0]==0):
zero_included=1
newline.append([0,1])
else:
newline.append(find_min(mult(firstline[x],secondline[x]),mult(secondline[x],newline[-1])))
firstline=newline
m +=1
#finding result(zeros in last element of array), and comparing it to 1 if there was there was 0 element somewhere
result=firstline[-1][1]
if zero_included!=0: result=min(result,1)
return(result)
The solution obtained perfect score.
all elements in A are neither dividable by 2 or 5; length of A is close to 500
all elements in A are dividable by either power of 2 or 5; length of A is close to 500