Skip to content

Commit bd10d49

Browse files
committed
Add solution #221
1 parent 9b15d4b commit bd10d49

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
218|[The Skyline Problem](./0218-the-skyline-problem.js)|Hard|
203203
219|[Contains Duplicate II](./0219-contains-duplicate-ii.js)|Easy|
204204
220|[Contains Duplicate III](./0220-contains-duplicate-iii.js)|Hard|
205+
221|[Maximal Square](./0221-maximal-square.js)|Medium|
205206
222|[Count Complete Tree Nodes](./0222-count-complete-tree-nodes.js)|Easy|
206207
224|[Basic Calculator](./0224-basic-calculator.js)|Hard|
207208
225|[Implement Stack using Queues](./0225-implement-stack-using-queues.js)|Easy|

solutions/0221-maximal-square.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* 221. Maximal Square
3+
* https://leetcode.com/problems/maximal-square/
4+
* Difficulty: Medium
5+
*
6+
* Given an m x n binary matrix filled with 0's and 1's, find the largest square containing only
7+
* 1's and return its area.
8+
*/
9+
10+
/**
11+
* @param {character[][]} matrix
12+
* @return {number}
13+
*/
14+
var maximalSquare = function(matrix) {
15+
const dp = [...new Array(matrix.length + 1)].map(() => new Array(matrix[0].length + 1).fill(0));
16+
let max = 0;
17+
18+
for (let i = 1; i < dp.length; i++) {
19+
for (let j = 1; j < dp[0].length; j++) {
20+
if (matrix[i - 1][j - 1] === '1') {
21+
max = Math.max(max, dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1);
22+
}
23+
}
24+
}
25+
26+
return max ** 2;
27+
};

0 commit comments

Comments
 (0)