
- 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
Program to find area of largest square of 1s in a given matrix in python
Suppose we have a binary matrix, we have to find largest square of 1s in that given matrix.
So, if the input is like
1 | 0 | 0 | 0 | 0 | 1 | 1 |
0 | 0 | 0 | 0 | 0 | 1 | 1 |
0 | 1 | 1 | 1 | 1 | 0 | 0 |
0 | 1 | 1 | 1 | 1 | 0 | 0 |
0 | 1 | 1 | 1 | 1 | 0 | 0 |
0 | 1 | 1 | 1 | 1 | 0 | 0 |
then the output will be 16.
To solve this, we will follow these steps −
- res := 0
- for i in range 0 to size of matrix, do
- res := maximum of res and matrix[i, 0]
- for i in range 0 to size of matrix[0], do
- res := maximum of res and matrix[0, i]
- for i in range 1 to row count of matrix, do
- for j in range 1 to column count of matrix, do
- if matrix[i, j] is same as 1, then
- matrix[i, j] = minimum of (matrix[i - 1, j], matrix[i - 1, j - 1] and matrix[i, j - 1]) + 1
- res = maximum of res and matrix[i, j]
- if matrix[i, j] is same as 1, then
- for j in range 1 to column count of matrix, do
- return res^2
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, matrix): res = 0 for i in range(len(matrix)): res = max(res, matrix[i][0]) for i in range(len(matrix[0])): res = max(res, matrix[0][i]) for i in range(1, len(matrix)): for j in range(1, len(matrix[0])): if matrix[i][j] == 1: matrix[i][j] = min(matrix[i - 1][j], matrix[i - 1][j - 1], matrix[i][j - 1]) + 1 res = max(res, matrix[i][j]) return res * res ob = Solution() matrix = [ [1, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 1, 1], [0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0, 0] ] print(ob.solve(matrix))
Input
matrix = [ [1, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 1, 1], [0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0, 0] ]
Output
16
- Related Articles
- Program to find area of largest island in a matrix in Python
- Program to count number of square submatix of 1s in the given matrix in C++
- Program to count number of square submatrices in given binary matrix in Python
- Program to find area of largest submatrix by column rearrangements in Python
- Program to find the transpose of given matrix in Python
- Program to find largest rectangle area under histogram in python
- Program to find number of rectangles that can form the largest square in Python
- Java Program to Find Area of Square
- Swift Program to Find Area of Square
- Kotlin Program to Find Area of Square
- Program to find number of distinct island shapes from a given matrix in Python
- Java program to find the area of a square
- Haskell Program to Find the Area of a Square
- C++ Program to Find Largest Rectangular Area in a Histogram
- Python Program to find out the determinant of a given special matrix

Advertisements