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 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.

Example:

Input:
[
  "0010",
  "0110",
  "0100"
]
and x = 0, y = 2

Output: 6

简单题,记录DFS到达的最大上下左右值。
class Solution {
private:
  int arr[4][2];
public:
  int minArea(vector<vector<char>>& image, int x, int y) {
    arr[0][0] = 1;
    arr[0][1] = 0;
    arr[1][0] = -1;
    arr[1][1] = 0;
    arr[2][0] = 0;
    arr[2][1] = 1;
    arr[3][0] = 0;
    arr[3][1] = -1;
    vector<int> ret;
    //ret.push_back();
    ret.push_back(INT_MAX);
    ret.push_back(INT_MIN);
    ret.push_back(INT_MAX);
    ret.push_back(INT_MIN);
    helper(image, x, y, ret);
    //cout << ret[0] << ret[1] << ret[2] << ret[3] << endl;
    return (ret[1] - ret[0] + 1) * (ret[3] - ret[2] + 1);
  }
  void helper(vector<vector<char>>& image, int x, int y, vector<int>& ret){
    if(image[x][y] == ‘0‘) return;
    image[x][y] = ‘0‘;
    //if(y == 0) cout << x << endl;
    ret[0] = min(x, ret[0]);
    ret[1] = max(x, ret[1]);
    ret[2] = min(y, ret[2]);
    ret[3] = max(y, ret[3]);
    for(int i=0; i<4; i++){
      if(x + arr[i][0] < image.size() && x + arr[i][0] >= 0 && y + arr[i][1] < image[0].size() && y + arr[i][1] >= 0){
        helper(image, x+arr[i][0], y+arr[i][1], ret);
      }
    }
  }
};


原文地址:https://www.cnblogs.com/ethanhong/p/10158694.html

时间: 2024-10-28 16:01:43

LC 302. Smallest Rectangle Enclosing Black Pixels【lock, hard】的相关文章

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 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

302. Smallest Rectangle Enclosing Black Pixels My Submissions QuestionEditorial Solution

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

LeetCode 302. Smallest Rectangle Enclosing Black Pixels

DFS 最一开始想到的就是dfs,从题目给的点开始dfs搜索. 时间复杂度为O(mn) class Solution { public: int i_min=INT_MAX, i_max=INT_MIN; int j_min=INT_MAX, j_max=INT_MIN; vector<vector<int>> dirs={{1,0},{-1,0},{0,1},{0,-1}}; int minArea(vector<vector<char>>& im

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 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

Smallest Rectangle Enclosing Black Pixels

Note: 约等于O(nlogn) public class Solution { /** * @param image a binary matrix with '0' and '1' * @param x, y the location of one of the black pixels * @return an integer */ public int minArea(char[][] image, int x, int y) { // Write your code here if(

hdu_1506:Largest Rectangle in a Histogram 【单调栈】

题目链接 对栈的一种灵活运用吧算是,希望我的注释写的足够清晰.. 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 5 const int N=100010; 6 int Stack[N]; //Stack[]为单调栈(即每次能入栈的元素值必比栈顶元素(若存在)要大) 7 int len[N]; //len[]与Stack[]同步 ,记录的是从Stack[]中对应元素起向左最大可延伸的宽度 8

LC 660. Remove 9 【lock, hard】

Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... So now, you will have a new integer sequence: 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, ... Given a positive integer n, you need to return the n-th integer after removing. Note that

【IOS笔记】Views

Views Because view objects are the main way your application interacts with the user, they have many responsibilities. Here are just a few: 通过view对象是与用户交互的主要方式,它们有很多责任,下面是其中一些: Layout and subview management   布局 A view defines its own default resizin

20150218【改进信号量】IMX257实现GPIO-IRQ中断按键获取键值驱动程序

[改进信号量]IMX257实现GPIO-IRQ中断按键获取键值驱动程序 2015-02-18 李海沿 前面我们使用POLL查询方式来实现GPIO-IRQ按键中断程序 这里我们来使用信号量,让我们的驱动同时只能有一个应用程序打开. 一.首先在前面代码的基础上来一个简单的信号 1.定义一个全局的整形变量 2.在打开函数中,每次进入打开函数canopen都自减1, 3.当我们不使用时,在realease 中canopen自加1 4.这样就实现了一个简单的信号量,我们编译,测试 当我们使用两个应用程序来