Answer to Question #198289 in Python for hemanth

Question #198289

Area of Rectangle

Given an MxN matrix filled with


X's and O's, find the largest rectangle containing only X's and return its area. If there are no Xs in the entire matrix print 0.Input

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.


Output

The output should be a single line containing the area of the maximum rectangle.


Explanation

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



1
Expert's answer
2021-05-25T10:48:35-0400
spaceSeparatedIntegers = input().split(' ')
N = int(spaceSeparatedIntegers[0])
M = int(spaceSeparatedIntegers[1])


matrix = []
for i in range(0, N):
    matrix.append(input().split(' '))


maxArea = 0
for i in range(0, N):
    for j in range(0, M):
       if matrix[i][j] == 'X':
           for k in range(0, N - i):
               for l in range(0, M - j):
                   found = True
                   for p in range(0, k + 1):
                       for z in range(0, l + 1):
                           if matrix[i+p][j+z] != 'X':
                              found = False
                   if found:
                      maxArea = max(maxArea, (k + 1) * (l + 1))




print(str(maxArea))




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS