Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7c721bb

Browse files
authored
Create Find the Minimum Area to Cover All Ones I.py
Signed-off-by: Erjan <erjan@users.noreply.github.com>
1 parent 742ae5e commit 7c721bb

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
You are given a 2D binary array grid. Find a rectangle with horizontal and vertical sides with the smallest area, such that all the 1's in grid lie inside this rectangle.
3+
4+
Return the minimum possible area of the rectangle.
5+
'''
6+
7+
class Solution:
8+
def minimumArea(self, grid: List[List[int]]) -> int:
9+
10+
res = 0
11+
12+
m = len(grid)
13+
n = len(grid[0])
14+
15+
minrow, maxrow = m,-1
16+
mincol,maxcol = n,-1
17+
18+
for i in range(m):
19+
for j in range(n):
20+
if grid[i][j] == 1:
21+
minrow = min(minrow,i)
22+
maxrow = max(maxrow, i)
23+
mincol = min(mincol,j)
24+
maxcol = max(maxcol,j)
25+
26+
return (maxrow-minrow+1)*(maxcol-mincol+1)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /