Skip to content

Commit ff68c27

Browse files
committed
update 221.maximal-square.java
1 parent 63ad93f commit ff68c27

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed

221.maximal-square.java

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
* @lc app=leetcode id=221 lang=java
3+
*
4+
* [221] Maximal Square
5+
*
6+
* https://leetcode.com/problems/maximal-square/description/
7+
*
8+
* algorithms
9+
* Medium (38.85%)
10+
* Total Accepted: 326.7K
11+
* Total Submissions: 840.2K
12+
* Testcase Example: '[["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]'
13+
*
14+
* Given an m x n binary matrix filled with 0's and 1's, find the largest
15+
* square containing only 1's and return its area.
16+
*
17+
*
18+
* Example 1:
19+
*
20+
*
21+
* Input: matrix =
22+
* [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
23+
* Output: 4
24+
*
25+
*
26+
* Example 2:
27+
*
28+
*
29+
* Input: matrix = [["0","1"],["1","0"]]
30+
* Output: 1
31+
*
32+
*
33+
* Example 3:
34+
*
35+
*
36+
* Input: matrix = [["0"]]
37+
* Output: 0
38+
*
39+
*
40+
*
41+
* Constraints:
42+
*
43+
*
44+
* m == matrix.length
45+
* n == matrix[i].length
46+
* 1 <= m, n <= 300
47+
* matrix[i][j] is '0' or '1'.
48+
*
49+
*
50+
*/
51+
52+
class Solution {
53+
public int maximalSquare(char[][] matrix) {
54+
int maxRow = matrix.length, maxCol = maxRow > 0 ? matrix[0].length : 0;
55+
int maxSqrLen = 0;
56+
for (int row = 0; row < maxRow; row++) {
57+
for (int col = 0; col < maxCol; col++) {
58+
59+
// Check for the maximum square size relative to current element
60+
// 1 - > ?
61+
// |
62+
// v
63+
// ?
64+
// Searching from left to right and top to bottom
65+
if (matrix[row][col] == '1') {
66+
int sqrLen = 1;
67+
boolean isExpandSquare = true;
68+
69+
// If square is expandable or not at the lower edge and/or right edge
70+
// of the matrix
71+
while (sqrLen + row < maxRow && sqrLen + col < maxCol && isExpandSquare) {
72+
73+
// Look for zero horizontally for expansion
74+
for (int i = col; i <= sqrLen + col; i++) {
75+
if (matrix[row + sqrLen][i] == '0') {
76+
isExpandSquare = false;
77+
break;
78+
}
79+
}
80+
81+
// Look for zero vertically for expansion
82+
for (int i = row; i <= sqrLen + row; i++) {
83+
if (matrix[i][col + sqrLen] == '0') {
84+
isExpandSquare = false;
85+
break;
86+
}
87+
}
88+
89+
if (isExpandSquare)
90+
sqrLen++;
91+
}
92+
93+
if (maxSqrLen < sqrLen) {
94+
maxSqrLen = sqrLen;
95+
}
96+
}
97+
}
98+
}
99+
return maxSqrLen * maxSqrLen;
100+
}
101+
}
102+
103+
104+
105+
// class Solution {
106+
// public int maximalSquare(char[][] matrix) {
107+
// // Check if matrix is empty
108+
// if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;
109+
// // Notes:
110+
// // 1's ands 0's are in Char format?
111+
//
112+
// // [["1","0","1","0","0"]
113+
// // ["1","0","1","1","1"]
114+
// // ["1","1","1","1","1"]
115+
// // ["1","0","0","1","0"]]
116+
// //
117+
// // [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
118+
//
119+
// // Approach:
120+
// // BRUTE FORCE:
121+
// // Loop over matrix m x n
122+
// // Check bottom row and right column for 1's
123+
// // Create a function for checking for surrounding 1's and 0's
124+
// // if (all 1's)
125+
// // updateArea;
126+
// // if (bottom row || right col nomore)
127+
// // break
128+
//
129+
// // Right and bottom search
130+
//
131+
// // First check if current index is 1, otherwise return 0
132+
// // Check if next nth binary is 1, if so, check the next nth, while recording
133+
// // how far it went so far. (this will be used how far to search mth wise or
134+
// // vertically)
135+
// // When it horizontal search reaches the closest zero break and go to the
136+
// // next mth and repeat certain times the furthest horizontal search reached
137+
//
138+
// // at the end, get the minimum value of the horizontal searches to get the
139+
// // biggest area of the matrix at the given start location
140+
//
141+
// int rowMax = matrix.length;
142+
// int colMax = matrix[0].length;
143+
// int maxSquareSide = 0;
144+
// for (int row = 0; row < rowMax - 1; row++) { // Exclude last row and col
145+
// for (int col = 0; col < colMax - 1; col++) {
146+
//
147+
// // find furthest non 0 from the right
148+
// int furthestNonZeroSubCol = findFurthestNonZeroSubCol(matrix, row, col, colMax);
149+
// maxSquareSide = Math.max(maxSquareSide,
150+
// evaluateOneSquare(matrix, row, col, furthestNonZeroSubCol));
151+
//
152+
// }
153+
// }
154+
// }
155+
//
156+
// public int findFurthestNonZeroSubCol(char[][] matrix, int row, int col, int colMax) {
157+
// int furthestNonZeroSubCol = 0;
158+
// for (int i = col; i < colMax; i++) {
159+
// if (matrix[row][i] == '0') break;
160+
// else furthestNonZeroSubCol++;
161+
// }
162+
// return furthestNonZeroSubCol;
163+
// }
164+
//
165+
// public int evaluateOneSquare(char[][] matrix, int row, int col, int size) {
166+
// while (++row <= size) {
167+
// int furthestNonZeroSubCol = findFurthestNonZeroSubCol(matrix, row, col, size);
168+
// if (furthestNonZeroSubCol <= size)
169+
// continue;
170+
// }
171+
// }
172+
// }

0 commit comments

Comments
 (0)