Area of Rectangle
Given an MxN matrix filled with
The first line of input will be containing two space-separated integers, denoting M and N.
The next M lines will contain N space-separated integers, denoting the elements of the matrix.
The output should be a single line containing the area of the maximum rectangle.
For example, if the given M, N and elements of matrix are as the following
4 5
X O X O O
X O X X X
X X X X X
X O O X O
The matrix from indices (1, 2) to (2, 4) has the maximum rectangle with
X. So the output should be the area of the maximum rectangle with X, which is 6.
Sample Input 1
4 5
X O X O O
X O X X X
X X X X X
X O O X O
Sample Output 1
6
Sample Input 2
3 3
O X X
O X X
O O O
Sample Output 2
4
dim = input("Enter number of rows and columns, separated by space: ").split(' ')
N = int(dim[0])
M = int(dim[1])
matrix = []
for i in range(0, N):
row = input("Enter row" + str(i+1) + " with X and O separated by spaces: ")
matrix.append(row.split(' '))
print()
for i in range(0, N):
print(str(matrix[i]))
print()
maxarea = 0
for i in range(0, N):
for j in range(0, M):
if matrix[i][j] == 'X':
nn = N - i
mm = M - j
for ii in range(0, nn):
for jj in range(0, mm):
found = True
for iii in range(0, ii + 1):
for jjj in range(0, jj + 1):
if matrix[i+iii][j+jjj] != 'X':
found = False
if found:
maxarea = max(maxarea, (ii + 1) * (jj + 1))
print(str(maxarea))
Comments
Dear Ramesh the code works well in python 3.9
This code is not working in python 3.9 can yu update a right answer for it
Leave a comment