<p>You are given a 2D <strong>binary</strong> array <code>grid</code>. Find a rectangle with horizontal and vertical sides with the<strong> smallest</strong> area, such that all the 1&#39;s in <code>grid</code> lie inside this rectangle.</p>

<p>Return the <strong>minimum</strong> possible area of the rectangle.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[0,1,0],[1,0,1]]</span></p>

<p><strong>Output:</strong> <span class="example-io">6</span></p>

<p><strong>Explanation:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect0.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 279px; height: 198px;" /></p>

<p>The smallest rectangle has a height of 2 and a width of 3, so it has an area of <code>2 * 3 = 6</code>.</p>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,0],[0,0]]</span></p>

<p><strong>Output:</strong> <span class="example-io">1</span></p>

<p><strong>Explanation:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/08/examplerect1.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 204px; height: 201px;" /></p>

<p>The smallest rectangle has both height and width 1, so its area is <code>1 * 1 = 1</code>.</p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= grid.length, grid[i].length &lt;= 1000</code></li>
	<li><code>grid[i][j]</code> is either 0 or 1.</li>
	<li>The input is generated such that there is at least one 1 in <code>grid</code>.</li>
</ul>

class Solution:
    def minimumArea(self, grid: List[List[int]]) -> int:
        # my approach based off immediate intuition
        # traverse from left to right, right to left, top to bottom, bottom to top
        # whenever we see a 1, mark the location. This will give us all the bounds
        # finally, get the distance between all the bounds to compute the area

        rows = len(grid)
        cols = len(grid[0]) 

        left_bound = 0
        right_bound = 0
        top_bound = 0
        bottom_bound = 0

        found = False

        # scan left to right
        for col in range(cols):
            for row in range(rows):
                if grid[row][col] == 1:
                    left_bound = col
                    found = True

            if found: break

        found = False

        # scan right to left
        for col in range(cols -1, -1, -1):
            for row in range(rows):
                if grid[row][col] == 1:
                    right_bound = col + 1
                    found = True
            
            if found: break

        found = False

        # scan top to bot
        for row in range(rows):
            for col in range(cols):
                if grid[row][col] == 1:
                    top_bound = row
                    found = True

            if found: break

        found = False

        # scan bot to top
        for row in range(rows -1, -1, -1):
            for col in range(cols):
                if grid[row][col] == 1:
                    bottom_bound = row + 1
                    found = True

            if found: break

        length = right_bound - left_bound
        height = bottom_bound - top_bound

        return length * height


        
2470