Understanding & Plan
According to the given example, I found out that when m = 4 and n = 6, the diagonals of the rectangles will cross 2*m = 8 little squares (means for each row it will cross exactly 2 little squares). Then we just need more cases and find out the pattern of m, n and number of squares the diagonal cross. For better understanding, we can draw the graph from n = m = 1 and come up with a table:
Then I found that there's a pattern when m = n, # of squares = m = n; when m > n, #of squares = n+1 if n is odd, = n if n is odd; when m<n, # of squares = m+n - 1 if n, m is even, # of squares = m+n - 2 if m,n is even
so we have the function:
def get_number_of_squares(m,n):
"""
(int, int) -> int
"""
if m == n:
return m
if m>n and n%2 == 0:
return 2m+1
elif m>n and n%2 != 0:
return 2m
if m<n and m%2 == 0 and n%2 == 0:
return m+n -2
elif m<n and m % 2 == 0 and not n % 2 == 0:
return m+n-1
elif m<n and not m%2 == 0 and n%2 == 0:
return m+n-1
elif m<n and not m%2 == 0 and not n%2 == 0:
return m+n-1