Leetcode - Smallest Rectangle Enclosing Black Pixels
My code:
public class Solution {
int row = 0;
int col = 0;
public int minArea(char[][] image, int x, int y) {
if (image == null || image.length == 0 || image[0].length == 0) {
return 0;
}
this.row = image.length;
this.col = image[0].length;
int top = searchRow(image, 0, x - 1, false);
int bottom = searchRow(image, x + 1, row - 1, true);
int left = searchCol(image, 0, y - 1, false);
int right = searchCol(image, y + 1, col - 1, true);
return (right - left + 1) * (bottom - top + 1);
}
private int searchRow(char[][] image, int begin, int end, boolean flag) {
while (begin <= end) {
int mid = begin + (end - begin) / 2;
boolean isBlack = false;
for (int j = 0; j < col; j++) {
if (image[mid][j] == '1') {
isBlack = true;
break;
}
}
if (isBlack) {
if (flag) {
begin = mid + 1;
}
else {
end = mid - 1;
}
}
else {
if (flag) {
end = mid - 1;
}
else {
begin = mid + 1;
}
}
}
return flag ? end : begin;
}
private int searchCol(char[][] image, int begin, int end, boolean flag) {
while (begin <= end) {
int mid = begin + (end - begin) / 2;
boolean isBlack = false;
for (int j = 0; j < row; j++) {
if (image[j][mid] == '1') {
isBlack = true;
break;
}
}
if (isBlack) {
if (flag) {
begin = mid + 1;
}
else {
end = mid - 1;
}
}
else {
if (flag) {
end = mid - 1;
}
else {
begin = mid + 1;
}
}
}
return flag ? end : begin;
}
}
reference:
https://discuss.leetcode.com/topic/29006/c-java-python-binary-search-solution-with-explanation
看了答案知道怎麼寫,然後自己寫了出來。
比答案要複雜些,當更容易理解些。
比如,行掃描。
對於(x, y) 上面的行,我們需要找到最小的那一行,他是black
對於(x, y) 下面的行,我們需要找到最大的那一行,他是black
列掃描也差不多。
然後邏輯就清楚了。
下面的問題在於,如果最大程度得程式碼複用。
Anyway, Good luck, Richardo! -- 10/12/2016
相關文章
- LeetCode-Smallest Rectangle Enclosing Black PixelsLeetCode
- Leetcode Maximal RectangleLeetCode
- LeetCode-Rectangle AreaLeetCode
- LeetCode-Perfect RectangleLeetCode
- Leetcode-Maximal RectangleLeetCode
- Maximal Rectangle leetcode javaLeetCodeJava
- Leetcode Kth Smallest Element in a BSTLeetCode
- LeetCode之Smallest Range I(Kotlin)LeetCodeKotlin
- LeetCode-Find K Pairs with Smallest SumsLeetCodeAI
- [LeetCode] 910. Smallest Range IILeetCode
- LeetCode-Kth Smallest Element in a Sorted MatrixLeetCode
- 【Leetcode】1081. Smallest Subsequence of Distinct CharactersLeetCode
- LeetCode-Max Sum of Rectangle No Larger Than KLeetCode
- [LeetCode] 378. Kth Smallest Element in a Sorted MatrixLeetCode
- [LeetCode] 230. Kth Smallest Element in a BSTLeetCode
- LeetCode 1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance??LeetCode
- [LeetCode] K-th Smallest Prime Fraction 第K小的質分數LeetCodeFraction質分數
- leetcode 掃描線專題 06-leetcode.391 perfect-rectangle 力扣.391 完美矩形LeetCode力扣
- WPF Rectangle ellipse
- leetcode 掃描線專題 06-leetcode.836 rectangle-overlap 力扣.836 矩形重疊LeetCode力扣
- Dart: The World's Smallest Laptop AdapterDartAPT
- No enclosing instance of type Outer is accessible.
- drawable 之 rectangle of shape示例
- Beaglebone Black教程Beaglebone Black的引腳分配
- Swift:Errors thrown from here are not handled because the enclosing catch is not exhaustiveSwiftError
- BeagleBone Black教程之BeagleBone Black裝置的連線
- Beaglebone Black教程BeagleBone Black安裝最新系統映像
- Rectangle for Mac視窗管理工具Mac
- [ARC179E] Rectangle Concatenation
- Lintcode387 The Smallest Difference solution 題解
- Python Enclosing作用域、裝飾器話聊上篇Python
- BeagleBone Black教程之BeagleBone Black使用到的Linux基礎Linux
- Rectangle Pro for Mac,視窗布局增強工具Mac
- Rectangle Pro for Mac(視窗布局增強工具)Mac
- 再學Java 之 解決No enclosing instance of type * is accessibleJava
- C. Black Circles
- 視窗增強工具:Rectangle Pro啟用版
- Python Enclosing作用域、閉包、裝飾器話聊下篇Python