bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 302. Smallest Rectangle Enclosing Black Pixels

302. Smallest Rectangle Enclosing Black Pixels

Leetcode Solutions

Binary Search to Find the Boundaries

  1. Define four binary search helper functions to find the left, right, top, and bottom boundaries of the black pixel region.
  2. For each boundary, perform a binary search on the projected 1D array (row or column).
  3. In each binary search, check the middle element (a row or column) to determine if it contains a black pixel.
  4. If a black pixel is found, continue searching towards the boundary; otherwise, search away from the boundary.
  5. Once all boundaries are found, calculate the area of the rectangle using the formula (right - left) * (bottom - top).
  6. Return the calculated area.
UML Thumbnail

Depth-First Search (DFS) to Find the Boundaries

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...