https://github.com/emahtab/maximal-square
Find the maximal square area
https://github.com/emahtab/maximal-square
dynamic-programming leetcode problem-solving
Last synced: about 1 month ago
JSON representation
Find the maximal square area
- Host: GitHub
- URL: https://github.com/emahtab/maximal-square
- Owner: eMahtab
- Created: 2020-03-22T06:40:51.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-31T07:24:12.000Z (almost 6 years ago)
- Last Synced: 2025-10-19T05:38:39.638Z (5 months ago)
- Topics: dynamic-programming, leetcode, problem-solving
- Homepage:
- Size: 3.91 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Maximal Square
## https://leetcode.com/problems/maximal-square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
```
Example:
Input:
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
Output: 4
```
# Implementation 1 :
```java
class Solution {
public int maximalSquare(char[][] matrix) {
if(matrix == null || matrix.length == 0)
return 0;
int maxSquareSize = 0;
int rows = matrix.length;
int cols = matrix[0].length;
int[][] dp = new int[rows + 1][cols + 1];
for(int row = 1; row <= rows; row++) {
for(int col = 1; col <= cols; col++) {
if(matrix[row - 1][col - 1] == '1') {
int min = Math.min(dp[row][col - 1], dp[row - 1][col]);
min = Math.min(min, dp[row - 1][col - 1]);
dp[row][col] = min + 1;
maxSquareSize = Math.max(maxSquareSize, dp[row][col]);
}
}
}
return maxSquareSize * maxSquareSize;
}
}
```
# References :
https://leetcode.com/articles/maximal-square