0
Leetcode Problem 302. Smallest Rectangle Enclosing Black Pixels
302. Smallest Rectangle Enclosing Black Pixels
AI Mock Interview
Leetcode Solutions
Binary Search to Find the Boundaries
Solution Idea
Algorithm Steps
Code Implementation
Complexity Analysis
Define four binary search helper functions to find the left, right, top, and bottom boundaries of the black pixel region.
For each boundary, perform a binary search on the projected 1D array (row or column).
In each binary search, check the middle element (a row or column) to determine if it contains a black pixel.
If a black pixel is found, continue searching towards the boundary; otherwise, search away from the boundary.
Once all boundaries are found, calculate the area of the rectangle using the formula
(right - left) * (bottom - top)
.
Return the calculated area.
Depth-First Search (DFS) to Find the Boundaries
Ask Question
Programming Language
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Full Screen
Copy Answer Code
Loading...