博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
302. Smallest Rectangle Enclosing Black Pixels
阅读量:6295 次
发布时间:2019-06-22

本文共 2997 字,大约阅读时间需要 9 分钟。

题目:

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 = 0, y = 2,
Return 6.

解答:

这道题我第一眼看到就想到dfs把每个点都扫一遍,找到最小最大边界值,然后作差相乘。但是我知道这是有冗余的,只需要边界值的话,并不需要扫每一个点,查找的话,最快还是binary search,所以有个第二种解法,但是边界还是很容易出错,要分清取舍。
1.DFS

private class Interval{    int min, max;    public Interval(int min, int max) {        this.min = min;        this.max = max;    }}public void search(char[][] image, int x, int y, Interval row, Interval col, boolean[][] visited) {    visited[x][y] = true;    row.max = Math.max(row.max, x);     row.min = Math.min(row.min, x);    col.max = Math.max(col.max, y);    col.min = Math.min(col.min, y);    int[][] dir = {
{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; for (int k = 0; k < dir.length; k++) { int i = x + dir[k][0], j = y + dir[k][1]; if (i < 0 || i > image.length - 1 || j < 0 || j > image[0].length - 1) { continue; } if (!visited[i][j] && image[i][j] == '1') { search(image, i, j, row, col, visited); } }}public int minArea(char[][] image, int x, int y) { if (image == null || image.length == 0 || image[0].length == 0) return 0; int m = image.length, n = image[0].length; Interval row = new Interval(m - 1, 0); Interval col = new Interval(n - 1, 0); boolean[][] visited = new boolean[m][n]; search(image, x, y, row, col, visited); return (row.max - row.min + 1) * (col.max - col.min + 1);}

2.Binary Search

public int searchColumns(char[][] image, int i, int j, int top, int bottom, String def) {    while (i != j) {        int k = top, mid = (i + j) / 2;        while (k < bottom && image[k][mid] == '0') ++k;        if (k < bottom && def.equals("min") || k >= bottom && def.equals("max")) {            j = mid;        } else {            i = mid + 1;        }    }    return i;}public int searchRows(char[][] image, int i, int j, int left, int right, String def) {    while (i != j) {        int k = left, mid = (i + j) / 2;        while (k < right && image[mid][k] == '0') k++;        if (k < right && def.equals("min") || k >= right && def.equals("max")) {            j = mid;        } else {            i = mid + 1;        }    }    return i;}public int minArea(char[][] image, int x, int y) {    if (image == null || image.length == 0 || image[0].length == 0) return 0;    int m = image.length, n = image[0].length;    int left = searchColumns(image, 0, y, 0, m, "min");    int right = searchColumns(image, y + 1, n, 0, m, "max");    int top = searchRows(image, 0, x, left, right, "min");    int bottom = searchRows(image, x + 1, m, left, right, "max");        return (right - left) * (bottom - top);}

转载地址:http://axvta.baihongyu.com/

你可能感兴趣的文章
同一台电脑上Windows 7和Ubuntu 14.04的CPU温度和GPU温度对比
查看>>
js数组的操作
查看>>
springmvc Could not write content: No serializer
查看>>
Python系语言发展综述
查看>>
新手 开博
查看>>
借助开源工具高效完成Java应用的运行分析
查看>>
163 yum
查看>>
第三章:Shiro的配置——深入浅出学Shiro细粒度权限开发框架
查看>>
80后创业的经验谈(转,朴实但实用!推荐)
查看>>
让Windows图片查看器和windows资源管理器显示WebP格式
查看>>
我的友情链接
查看>>
vim使用点滴
查看>>
embedded linux学习中几个需要明确的概念
查看>>
mysql常用语法
查看>>
Morris ajax
查看>>
【Docker学习笔记(四)】通过Nginx镜像快速搭建静态网站
查看>>
ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务
查看>>
<转>云主机配置OpenStack使用spice的方法
查看>>
java jvm GC 各个区内存参数设置
查看>>
[使用帮助] PHPCMS V9内容模块PC标签调用说明
查看>>