LC: 302. Smallest Rectangle Enclosing Black Pixels
https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/
302. Smallest Rectangle Enclosing Black Pixels
You are given an image that is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel.
The black pixels are connected (i.e., there is only one black region). Pixels are connected horizontally and vertically.
Given two integers x and y that represent the location of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that encloses all black pixels.
Example 1:![]()
Input: image = [["0","0","1","0"],["0","1","1","0"],["0","1","0","0"]], x = 0, y = 2
Output: 6Example 2:
Input: image = [["1"]], x = 0, y = 0
Output: 1Constraints:
m == image.lengthn == image[i].length1 <= m, n <= 100image[i][j]is either'0'or'1'.1 <= x < m1 <= y < nimage[x][y] == '1'.The black pixels in the
imageonly form one component.
302. Smallest Rectangle Enclosing Black Pixels:
The Essence:
Ein solcher Rechteck entspricht dem Rechteck, der das linkste, rechteste, oberste und niedrigste Pixel enthält. Da die Pixels zusammenhängend sind, kann man sie irgendwie traversieren, um diese Punkte zu finden.
Details:
Man kann entweder Tiefensuche oder Breitensuche verwenden. Außerdem steht eine Methode, die diese Indizes durch binäre Suche bestimmt, zur Verfügung.
Solution(s) and Further Explanation:
Default Code:
Last updated