Answer to Question #194010 in Python for J NAGAMANI

Question #194010

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


1
Expert's answer
2021-05-17T03:35:46-0400
def max_x_area(matrix):


	r = len(matrix)
	c = len(matrix[0])
	area = 0


	def all_x(mat):


		for row in mat:
			for el in row:
				if el != 'X':
					return False
		return True


	def sub_seq(lst):


		s = len(lst)
		for start in range(s):
			for end in range(start+1, s+1):
				yield (start, end)


	for s_r, e_r in sub_seq(list(range(r))):
		for s_c, e_c in sub_seq(list(range(c))):
			if all_x([i[s_c:e_c] for i in matrix[s_r:e_r]]):
				tmp_area = (e_r - s_r) * (e_c - s_c)
				if tmp_area > area:
					area = tmp_area
	print(area)




while True:
	try:
		m, n = (map(int, input().split()))
	except ValueError:
		print('enter two space separated in M N ')
		continue
	matrix = []
	for i in range(m):
		matrix.append(list(input().split()))
	max_x_area(matrix)

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