
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Find the largest rectangle of 1’s with swapping of columns allowed in Python
Suppose we have a binary matrix, we have to find the largest rectangle of all 1's in that given matrix. The rectangle can be built by swapping or exchanging any pair of columns of that matrix.
So, if the input is like
1 | 0 | 0 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 0 | 1 | 0 |
then the output will be the 6 in this case. The rectangle can be generating by exchanging column 1 with 3. The matrix after exchanging will be −
0 | 0 | 1 | 1 | 0 |
0 | 0 | 1 | 1 | 1 |
1 | 0 | 1 | 1 | 0 |
To solve this, we will follow these steps −
row := size of mat
col := size of mat[0]
temp := a matrix of order (row + 1) x (col + 1), and fill with 0
for i in range 0 to col, do
temp[0, i] := mat[0, i]
for j in range 1 to row, do
if mat[j, i] is same as 0, then
temp[j, i] := 0
otherwise,
temp[j, i] := temp[j - 1, i] + 1
for i in range 0 to row, do
cnt := an array of size (row + 1), and filled with 0
for j in range 0 to col, increase by 1, do
cnt[temp[i, j]] := cnt[temp[i, j]] + 1
col_no := 0
j := row
while j >= 0, do
if cnt[j] > 0, then
for k in range 0 to cnt[j], do
temp[i, col_no] := j
col_no := col_no + 1
j := j - 1
area_maximum := 0
for i in range 0 to row, do
for j in range 0 to col, do
area_current :=(j + 1) * temp[i, j]
if area_current > area_maximum, then
area_maximum := area_current
return area_maximum
Example
Let us see the following implementation to get better understanding −
def maxArea(mat): row = len(mat) col = len(mat[0]) temp = [[0 for i in range(col + 1)] for i in range(row + 1)] for i in range(0, col): temp[0][i] = mat[0][i] for j in range(1, row): if ((mat[j][i] == 0)): temp[j][i] = 0 else: temp[j][i] = temp[j - 1][i] + 1 for i in range(0, row): cnt = [0 for i in range(row + 1)] for j in range(0, col, 1): cnt[temp[i][j]] += 1 col_no = 0 j = row while(j >= 0): if (cnt[j] > 0): for k in range(0, cnt[j]): temp[i][col_no] = j col_no += 1 j -= 1 area_maximum = 0 for i in range(0, row): for j in range(0, col): area_current = (j + 1) * temp[i][j] if (area_current > area_maximum): area_maximum = area_current return area_maximum mat = [ [0, 0, 1, 1, 0], [0, 0, 1, 1, 1], [1, 0, 1, 1, 0]] print("Area : ",maxArea(mat))
Input
[ [1, 0, 0, 1, 0], [1, 0, 0, 1, 1], [1, 1, 0, 1, 0]]
Output
Area : 2
- Related Articles
- Check if it is possible to sort an array with conditional swapping of adjacent allowed in Python
- Python program to find the length of the largest consecutive 1's in Binary Representation of a given string.
- Largest Rectangle in Histogram in Python
- Largest number with one swap allowed in C++
- Program to find largest rectangle area under histogram in python
- Python map function to find the row with the maximum number of 1’s
- Largest number with binary representation is m 1’s and m-1 0’s in C++
- How to set the minimum allowed scale value of Rectangle using FabricJS?
- Find the cordinates of the fourth vertex of a rectangle with given 3 vertices in Python
- C++ Largest Subtree having Equal No of 1's and 0's
- Python program using map function to find row with maximum number of 1's
- Python program using the map function to find a row with the maximum number of 1's
- If the perimeter of rectangle is 120 cm and it's length is 40 cm. Find the breadth of rectangle?
- Program to find longest number of 1s after swapping one pair of bits in Python
- Find the Pattern of 1’s inside 0’s using C++
