Question

An image 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 the location (x, y) of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that encloses all black pixels.

For example, given the following image:

[
"0010",
"0110",
"0100"
]

and x = 0y = 2,

Return 6.

Solution 1 -- DFS

因为题目里说输入里只有一个全是1的联通域。所以这个问题转化为求值为1的点的left most position, right most position, top most position, bottom most position。

可以用DFS,遍历找出所有值为1的点,更新以上四个position value。

Time complexity O(mn)

 public class Solution {
private int right;
private int left;
private int top;
private int bottom;
private final int[][] directions = {{0,-1},{0,1},{1,0},{-1,0}}; public int minArea(char[][] image, int x, int y) {
if (image == null || image.length < 1) {
return 0;
}
int m = image.length, n = image[0].length;
bottom = 0;
top = m - 1;
right = 0;
left = n - 1;
boolean[][] visited = new boolean[m][n];
dfs(image, x, y, visited);
int area = 0;
if (top <= bottom && left <= right) {
area = (bottom - top + 1) * (right - left + 1);
}
return area;
} private void dfs(char[][] image, int x, int y, boolean[][] visited) {
if (x < 0 || x >= image.length || y < 0 || y >= image[0].length) {
return;
}
if (visited[x][y]) {
return;
}
if (image[x][y] != '1') {
return;
}
visited[x][y] = true;
// update left, right, top, bottom
if (x > bottom) {
bottom = x;
}
if (x < top) {
top = x;
}
if (y > right) {
right = y;
}
if (y < left) {
left = y;
}
// dfs
for (int i = 0; i < 4; i++) {
dfs(image, x + directions[i][0], y + directions[i][1], visited);
}
} }

Solution 2 -- Binary Search

Discuss里给出了二分查找的方法。

Smallest Rectangle Enclosing Black Pixels 解答的更多相关文章

  1. [LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  2. Smallest Rectangle Enclosing Black Pixels

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  3. LeetCode Smallest Rectangle Enclosing Black Pixels

    原题链接在这里:https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/ 题目: An image is rep ...

  4. 302. Smallest Rectangle Enclosing Black Pixels

    题目: An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The b ...

  5. [Locked] Smallest Rectangle Enclosing Black Pixels

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  6. [Swift]LeetCode302. 包含黑色像素的最小矩形 $ Smallest Rectangle Enclosing Black Pixels

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  7. 【leetcode】302.Smallest Rectangle Enclosing Black Pixels

    原题 An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The bl ...

  8. LC 302. Smallest Rectangle Enclosing Black Pixels【lock, hard】

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  9. Kth Smallest Element in a BST 解答

    Question Given a binary search tree, write a function kthSmallest to find the kth smallest element i ...

随机推荐

  1. hsql使用架构包启动数据库

    一.通常我们平时启动就是直接通过hsql.jar来进行启动 java -cp hsqldb.jar org.hsqldb.util.DatabaseManagerSwing java -cp hsql ...

  2. 原生sql语句执行

    public function Text() { $nation = D("Nation"); $sqla = "select * from nation"; ...

  3. poj 2049 Let it Bead(polya模板)

      Description Cannery Row percent of the target audience insists that the bracelets be unique. (Just ...

  4. pyqt记录内容(音乐播放器)

    #这是UI文件 # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'AudioPlayerDia ...

  5. UIScrollView使用autolayout 垂直滚动

    转自:http://dadage456.blog.163.com/blog/static/30310744201491141752716 1.创建一个空白的UIViewController .将UIS ...

  6. delphi 简单的删除字符串尾部数字的代码

    delphi  简单的删除字符串尾部数字的代码 方式一: function FilterShowName(const sName: String): String; var I: Integer; b ...

  7. linux自动交互工具expect,tcl安装和安装包,以及自动互信脚本

    linux自动交互工具expect,tcl安装,以及自动互信脚本 工作中需要对几十台服务器做自动互信,无意中发现expect命令,研究一番. 在网上找了许多资料也没有安装成功,摸索着总算成功了.现分享 ...

  8. 初识QML学习机制

    在QML中,一个用户界面被指定为具有属性的对象树,这使得Qt更加便于很少或没有编程经验的人使用,JavaScript在QML中作为一种脚本语言,对QML进行逻辑方面的编程. AD:WOT2015 互联 ...

  9. 解决android自带textview右侧文字不能对齐的问题

    package com.sixin.view; import android.content.Context; import android.graphics.Canvas; import andro ...

  10. Jmail组件发送邮件说明ASP.NET

    ASP.Net环境下使用Jmail组件发送邮件2008-01-25 18:59实现过程: 不同于在Asp中使用Jmail,直接使用 Server.CreateObject("Jmail.Me ...