302. Smallest Rectangle Enclosing Black Pixels
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the area of the smallest axis-aligned rectangle that encloses a single connected region of black pixels in a binary matrix. A crucial constraint is that the algorithm's runtime must be less than O(mn), where m and n are the dimensions of the matrix. This rules out simple solutions like iterating through the entire grid or performing a standard Breadth-First Search (BFS) / Depth-First Search (DFS) on all pixels, as their worst-case complexity is O(mn). The sub-linear complexity requirement strongly suggests that a binary search-based approach is necessary.
Solution
Smallest Rectangle Enclosing Black Pixels in Python
class Solution:
def minArea(self, image: List[List[str]], x: int, y: int) -> int:
m, n = len(image), len(image[0])
# Helper function to binary search for the top boundary (first row with a '1')
def search_top(low, high):
while low < high:
mid = (low + high) // 2
# Check if there is any '1' in the mid-th row
if '1' in image[mid]:
high = mid # Potential answer, try to find an earlier one
else:
low = mid + 1 # Top must be below this row
return low
# Helper function to binary search for the bottom boundary (last row with a '1')
def search_bottom(low, high):
while low < high:
# Bias mid to the right to avoid infinite loop
mid = (low + high + 1) // 2
if '1' in image[mid]:
low = mid # Potential answer, try to find a later one
else:
high = mid - 1 # Bottom must be above this row
return low
# Helper function to binary search for the left boundary (first column with a '1')
def search_left(low, high):
while low < high:
mid = (low + high) // 2
# Check if there is any '1' in the mid-th column
if any(image[i][mid] == '1' for i in range(m)):
high = mid # Potential answer, try to find an earlier one
else:
low = mid + 1 # Left must be to the right of this column
return low
# Helper function to binary search for the right boundary (last column with a '1')
def search_right(low, high):
while low < high:
# Bias mid to the right to avoid infinite loop
mid = (low + high + 1) // 2
if any(image[i][mid] == '1' for i in range(m)):
low = mid # Potential answer, try to find a later one
else:
high = mid - 1 # Right must be to the left of this column
return low
# Perform binary search for each of the four boundaries
top = search_top(0, x)
bottom = search_bottom(x, m - 1)
left = search_left(0, y)
right = search_right(y, n - 1)
return (right - left + 1) * (bottom - top + 1)
Smallest Rectangle Enclosing Black Pixels in Java
class Solution {
public int minArea(char[][] image, int x, int y) {
int m = image.length;
int n = image[0].length;
// Binary search for the four boundaries
int top = searchTop(image, 0, x, m, n);
int bottom = searchBottom(image, x, m - 1, m, n);
int left = searchLeft(image, 0, y, m, n);
int right = searchRight(image, y, n - 1, m, n);
return (right - left + 1) * (bottom - top + 1);
}
// Binary search for the top boundary (first row with a '1')
private int searchTop(char[][] image, int low, int high, int m, int n) {
while (low < high) {
int mid = low + (high - low) / 2;
boolean hasOne = false;
for (int j = 0; j < n; j++) {
if (image[mid][j] == '1') {
hasOne = true;
break;
}
}
if (hasOne) {
high = mid; // Potential answer, try to find an earlier one
} else {
low = mid + 1; // Top must be below this row
}
}
return low;
}
// Binary search for the bottom boundary (last row with a '1')
private int searchBottom(char[][] image, int low, int high, int m, int n) {
while (low < high) {
// Bias mid to the right to avoid infinite loop
int mid = low + (high - low + 1) / 2;
boolean hasOne = false;
for (int j = 0; j < n; j++) {
if (image[mid][j] == '1') {
hasOne = true;
break;
}
}
if (hasOne) {
low = mid; // Potential answer, try to find a later one
} else {
high = mid - 1; // Bottom must be above this row
}
}
return low;
}
// Binary search for the left boundary (first column with a '1')
private int searchLeft(char[][] image, int low, int high, int m, int n) {
while (low < high) {
int mid = low + (high - low) / 2;
boolean hasOne = false;
for (int i = 0; i < m; i++) {
if (image[i][mid] == '1') {
hasOne = true;
break;
}
}
if (hasOne) {
high = mid; // Potential answer, try to find an earlier one
} else {
low = mid + 1; // Left must be to the right of this column
}
}
return low;
}
// Binary search for the right boundary (last column with a '1')
private int searchRight(char[][] image, int low, int high, int m, int n) {
while (low < high) {
// Bias mid to the right to avoid infinite loop
int mid = low + (high - low + 1) / 2;
boolean hasOne = false;
for (int i = 0; i < m; i++) {
if (image[i][mid] == '1') {
hasOne = true;
break;
}
}
if (hasOne) {
low = mid; // Potential answer, try to find a later one
} else {
high = mid - 1; // Right must be to the left of this column
}
}
return low;
}
}
Smallest Rectangle Enclosing Black Pixels in C++
class Solution {
public:
int minArea(vector<vector<char>>& image, int x, int y) {
int m = image.size();
int n = image[0].size();
// Binary search for the four boundaries
int top = searchTop(image, 0, x, m, n);
int bottom = searchBottom(image, x, m - 1, m, n);
int left = searchLeft(image, 0, y, m, n);
int right = searchRight(image, y, n - 1, m, n);
return (right - left + 1) * (bottom - top + 1);
}
private:
// Binary search for the top boundary (first row with a '1')
int searchTop(const vector<vector<char>>& image, int low, int high, int m, int n) {
while (low < high) {
int mid = low + (high - low) / 2;
bool hasOne = false;
for (int j = 0; j < n; ++j) {
if (image[mid][j] == '1') {
hasOne = true;
break;
}
}
if (hasOne) {
high = mid; // Potential answer, try to find an earlier one
} else {
low = mid + 1; // Top must be below this row
}
}
return low;
}
// Binary search for the bottom boundary (last row with a '1')
int searchBottom(const vector<vector<char>>& image, int low, int high, int m, int n) {
while (low < high) {
// Bias mid to the right to avoid infinite loop
int mid = low + (high - low + 1) / 2;
bool hasOne = false;
for (int j = 0; j < n; ++j) {
if (image[mid][j] == '1') {
hasOne = true;
break;
}
}
if (hasOne) {
low = mid; // Potential answer, try to find a later one
} else {
high = mid - 1; // Bottom must be above this row
}
}
return low;
}
// Binary search for the left boundary (first column with a '1')
int searchLeft(const vector<vector<char>>& image, int low, int high, int m, int n) {
while (low < high) {
int mid = low + (high - low) / 2;
bool hasOne = false;
for (int i = 0; i < m; ++i) {
if (image[i][mid] == '1') {
hasOne = true;
break;
}
}
if (hasOne) {
high = mid; // Potential answer, try to find an earlier one
} else {
low = mid + 1; // Left must be to the right of this column
}
}
return low;
}
// Binary search for the right boundary (last column with a '1')
int searchRight(const vector<vector<char>>& image, int low, int high, int m, int n) {
while (low < high) {
// Bias mid to the right to avoid infinite loop
int mid = low + (high - low + 1) / 2;
bool hasOne = false;
for (int i = 0; i < m; ++i) {
if (image[i][mid] == '1') {
hasOne = true;
break;
}
}
if (hasOne) {
low = mid; // Potential answer, try to find a later one
} else {
high = mid - 1; // Right must be to the left of this column
}
}
return low;
}
};
Smallest Rectangle Enclosing Black Pixels in TypeScript
function minArea(image: string[][], x: number, y: number): number {
const m = image.length;
const n = image[0].length;
// Binary search for the top boundary (first row with a '1')
const searchTop = (low: number, high: number): number => {
while (low < high) {
const mid = Math.floor((low + high) / 2);
if (image[mid].includes('1')) {
high = mid; // Potential answer, try to find an earlier one
} else {
low = mid + 1; // Top must be below this row
}
}
return low;
};
// Binary search for the bottom boundary (last row with a '1')
const searchBottom = (low: number, high: number): number => {
while (low < high) {
// Bias mid to the right to avoid infinite loop
const mid = Math.floor((low + high + 1) / 2);
if (image[mid].includes('1')) {
low = mid; // Potential answer, try to find a later one
} else {
high = mid - 1; // Bottom must be above this row
}
}
return low;
};
// Binary search for the left boundary (first column with a '1')
const searchLeft = (low: number, high: number): number => {
while (low < high) {
const mid = Math.floor((low + high) / 2);
let hasOne = false;
for (let i = 0; i < m; i++) {
if (image[i][mid] === '1') {
hasOne = true;
break;
}
}
if (hasOne) {
high = mid; // Potential answer, try to find an earlier one
} else {
low = mid + 1; // Left must be to the right of this column
}
}
return low;
};
// Binary search for the right boundary (last column with a '1')
const searchRight = (low: number, high: number): number => {
while (low < high) {
// Bias mid to the right to avoid infinite loop
const mid = Math.floor((low + high + 1) / 2);
let hasOne = false;
for (let i = 0; i < m; i++) {
if (image[i][mid] === '1') {
hasOne = true;
break;
}
}
if (hasOne) {
low = mid; // Potential answer, try to find a later one
} else {
high = mid - 1; // Right must be to the left of this column
}
}
return low;
};
// Perform binary search for each of the four boundaries
const top = searchTop(0, x);
const bottom = searchBottom(x, m - 1);
const left = searchLeft(0, y);
const right = searchRight(y, n - 1);
return (right - left + 1) * (bottom - top + 1);
};
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.