Skip to content

Commit 551586c

Browse files
committed
Add solution #3195
1 parent ed61374 commit 551586c

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2667,6 +2667,7 @@
26672667
3183|[The Number of Ways to Make the Sum](./solutions/3183-the-number-of-ways-to-make-the-sum.js)|Medium|
26682668
3189|[Minimum Moves to Get a Peaceful Board](./solutions/3189-minimum-moves-to-get-a-peaceful-board.js)|Medium|
26692669
3191|[Minimum Operations to Make Binary Array Elements Equal to One I](./solutions/3191-minimum-operations-to-make-binary-array-elements-equal-to-one-i.js)|Medium|
2670+
3195|[Find the Minimum Area to Cover All Ones I](./solutions/3195-find-the-minimum-area-to-cover-all-ones-i.js)|Medium|
26702671
3199|[Count Triplets with Even XOR Set Bits I](./solutions/3199-count-triplets-with-even-xor-set-bits-i.js)|Easy|
26712672
3201|[Find the Maximum Length of Valid Subsequence I](./solutions/3201-find-the-maximum-length-of-valid-subsequence-i.js)|Medium|
26722673
3202|[Find the Maximum Length of Valid Subsequence II](./solutions/3202-find-the-maximum-length-of-valid-subsequence-ii.js)|Medium|
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 3195. Find the Minimum Area to Cover All Ones I
3+
* https://leetcode.com/problems/find-the-minimum-area-to-cover-all-ones-i/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 2D binary array grid. Find a rectangle with horizontal and vertical
7+
* sides with the smallest area, such that all the 1's in grid lie inside this rectangle.
8+
*
9+
* Return the minimum possible area of the rectangle.
10+
*/
11+
12+
/**
13+
* @param {number[][]} grid
14+
* @return {number}
15+
*/
16+
var minimumArea = function(grid) {
17+
const rows = grid.length;
18+
const cols = grid[0].length;
19+
let minRow = rows;
20+
let maxRow = -1;
21+
let minCol = cols;
22+
let maxCol = -1;
23+
24+
for (let i = 0; i < rows; i++) {
25+
for (let j = 0; j < cols; j++) {
26+
if (grid[i][j] === 1) {
27+
minRow = Math.min(minRow, i);
28+
maxRow = Math.max(maxRow, i);
29+
minCol = Math.min(minCol, j);
30+
maxCol = Math.max(maxCol, j);
31+
}
32+
}
33+
}
34+
35+
return (maxRow - minRow + 1) * (maxCol - minCol + 1);
36+
};

0 commit comments

Comments
 (0)