[CareerCup] 18.11 Maximum Subsquare 最大子方形

18.11 Imagine you have a square matrix, where each cell (pixel) is either black or white. Design an algorithm to find the maximum subsquare such that all four borders are filled with black pixels.

LeetCode上的原题,请参见我之前的解法Maximal Square。书上给了两种解法,但是比较长:

解法一:

class Subsquare {
public:
    int row, col, size;
    Subsquare(int r, int c, int sz): row(r), col(c), size(sz) {}
    void print() {
        cout << "(" << row << ", " << col << ", " << size << ")" << endl;
    }
};

bool is_square(vector<vector<int>> &matrix, int row, int col, int size) {
    for (int j = 0; j < size; ++j) {
        if (matrix[row][col + j] == 1) return false;
        if (matrix[row + size - 1][col + j] == 1) return false;
    }
    for (int i = 1; i < size - 1; ++i) {
        if (matrix[row + i][col] == 1) return false;
        if (matrix[row + i][col + size - 1] == 1) return false;
    }
    return true;
}

Subsquare* find_square_with_size(vector<vector<int>> &matrix, int squareSize) {
    int cnt = matrix.size() - squareSize + 1;
    for (int row = 0; row < cnt; ++row) {
        for (int col = 0; col < cnt; ++col) {
            if (is_square(matrix, row, col, squareSize)) {
                return new Subsquare(row, col, squareSize);
            }
        }
    }
    return NULL;
}

Subsquare* find_square(vector<vector<int>> &matrix) {
    for (int i = matrix.size(); i >= 1; --i) {
        Subsquare *square = find_square_with_size(matrix, i);
        if (square) return square;
    }
    return NULL;
}

解法二:

class Subsquare {
public:
    int row, col, size;
    Subsquare(int r, int c, int sz): row(r), col(c), size(sz) {}
    void print() {
        cout << "(" << row << ", " << col << ", " << size << ")" << endl;
    }
};

class SquareCell {
public:
    int zerosRight = 0, zerosBelow = 0;
    SquareCell(int right, int below): zerosRight(right), zerosBelow(below){}
    void setZerosRight(int right) {
        zerosRight = right;
    }
    void setZerosBelow(int below) {
        zerosBelow = below;
    }
};

bool is_square(vector<vector<SquareCell*>> &matrix, int row, int col, int size) {
    SquareCell *topLeft = matrix[row][col];
    SquareCell *topRight = matrix[row][col + size - 1];
    SquareCell *bottomRight = matrix[row + size - 1][col];
    if (topLeft->zerosRight < size) return false;
    if (topLeft->zerosBelow < size) return false;
    if (topRight->zerosBelow < size) return false;
    if (bottomRight->zerosRight < size) return false;
    return true;
}

vector<vector<SquareCell*>> process_square(vector<vector<int>> &matrix) {
    vector<vector<SquareCell*>> res(matrix.size(), vector<SquareCell*>(matrix.size()));
    for (int r = matrix.size() - 1; r >= 0; --r) {
        for (int c = matrix.size() - 1; c >= 0; --c) {
            int rightZeros = 0, belowZeros = 0;
            if (matrix[r][c] == 0) {
                ++rightZeros;
                ++belowZeros;
                if (c + 1 < matrix.size()) {
                    SquareCell *pre = res[r][c + 1];
                    rightZeros += pre->zerosRight;
                }
                if (r + 1 < matrix.size()) {
                    SquareCell *pre = res[r + 1][c];
                    belowZeros += pre->zerosBelow;
                }
            }
            res[r][c] = new SquareCell(rightZeros, belowZeros);
        }
    }
    return res;
}

Subsquare* find_square_with_size(vector<vector<SquareCell*>> &processed, int square_size) {
    int cnt = processed.size() - square_size + 1;
    for (int row = 0; row < cnt; ++row) {
        for (int col = 0; col < cnt; ++col) {
            if (is_square(processed, row, col, square_size)) {
                return new Subsquare(row, col, square_size);
            }
        }
    }
    return NULL;
}

Subsquare* find_square(vector<vector<int>> &matrix) {
    vector<vector<SquareCell*>> processed = process_square(matrix);
    // cout << "here" << endl;
    for (int i = matrix.size(); i >= 1; --i) {
        Subsquare *square = find_square_with_size(processed, i);
        if (square) return square;
    }
    return NULL;
}

CareerCup All in One 题目汇总

时间: 2024-11-05 11:27:49

[CareerCup] 18.11 Maximum Subsquare 最大子方形的相关文章

[Coding Made Simple] Maximum Subsquare surrounded by &#39;X&#39;

Given a 2D matrix where every element is either 'O' or 'X', find the largest subsquare surrounded by 'X'. Examples: Input: mat[N][N] = { {'X', 'O', 'X', 'X', 'X'}, {'X', 'X', 'X', 'X', 'X'}, {'X', 'X', 'O', 'X', 'O'}, {'X', 'X', 'X', 'X', 'X'}, {'X',

日本IT行业劳动力缺口达22万 在日中国留学生迎来就业好时机 2017/07/18 11:25:09

作者:倪亚敏 来源:日本新华侨报 发布时间:2017/07/18 11:25:09   据日本政府提供的数据,日本2018年应届毕业生的"求人倍率"已经达到了1.78倍.换言之,就是100名大学生里有178个就业岗位可选择,即使所有大学毕业生都进入劳动力市场,也还有78个岗位空缺.劳动力现状是寒冷冬天,而学生们确实迎来了就业春天. 在众多岗位当中,日本的信息技术(IT)领域采用的外国人留学生最多.去年,已经在日本企业就职的留学生当中(除去制造行业),27.3%的毕业生进入了IT行业.根

18.11 LVS DR模式搭建;18.12 keepalived lvs

扩展 : haproxy+keepalived : http://blog.csdn.net/xrt95050/article/details/40926255 nginx.lvs.haproxy比较 : http://www.csdn.net/article/2014-07-24/2820837 keepalived中自定义脚本 vrrp_script : http://my.oschina.net/hncscwc/blog/158746 lvs dr模式只使用一个公网ip的实现方法 : ht

18.11 LVS DR模式搭建18.12 keepalived + LVS

18.11 LVS DR模式搭建首先将两台rs的网关改回来,因为在做nat模式的时候更改了网关vip需要绑定在所有的机器上在分以器dir上编辑脚本vim /usr/local/sbin/lvs_dr.sh内容#! /bin/bashecho 1 > /proc/sys/net/ipv4/ip_forwardipv=/usr/sbin/ipvsadmvip=192.168.133.200rs1=192.168.133.132rs2=192.168.133.133#注意这里的网卡名字ifdown e

18.11 LVS DR模式搭建 18.12 keepalived + LVS

18.11 LVS DR模式搭建 准备三台机器,然后设置一下ip,网关也需要设置一下,然后所有的机器都需要绑定一下vip. 然后dir上需要编辑一个脚本,,内容如上. 是打开端口转发. 设置变量,后面都用前面的字符代替后面的地址或ip. 绑定vip. 把网卡重启一下,重启过程中会把之前设置的ip清空掉,为的是再次执行这个脚本就不会再重新设置这个ip了. 添加网关. 是规则. 然后我们执行开启. 然后我们还需要对两个rs机器也写一个脚本. 然后执行 然后我们可以看一下,可以看到有一个133.200

18.11 ROM、RAM、DRAM、SRAM和FLASH区别

ROM(Read Only Memory)和RAM(Random Access Memory)指的都是半导体存储器.ROM在系统停止供电的时候仍然可以保持数据,而RAM通常都是在掉电之后就丢失数据,但是访问速度快.典型的RAM就是计算机的内存. RAM有两大类,一种称为静态RAM(Static RAM/SRAM),SRAM速度非常快,是目前读写最快的存储设备,但是也非常昂贵,所以只在要求很苛刻的地方使用,譬如CPU的一级缓冲,二级缓冲.另一种称为动态RAM(Dynamic RAM/DRAM),D

[CareerCup] 18.12 Largest Sum Submatrix 和最大的子矩阵

18.12 Given an NxN matrix of positive and negative integers, write code to find the submatrix with the largest possible sum. 这道求和最大的子矩阵,跟LeetCode上的Maximum Size Subarray Sum Equals k和Maximum Subarray很类似.这道题不建议使用brute force的方法,因为实在是不高效,我们需要借鉴上面LeetCode

[CareerCup] 18.4 Count Number of Two 统计数字2的个数

18.4 Write a method to count the number of 2s between 0 and n. 这道题给了我们一个整数n,让我们求[0,n]区间内所有2出现的个数,比如如果n=20,那么满足题意的是2, 12, 20,那么返回3即可.LeetCode上有一道很类似的题Factorial Trailing Zeroes,但是那道题求5的个数还包括了因子中的5,比如10里面也有5,这是两题的不同之处.那么首先这题可以用brute force来解,我们对区间内的每一个数字

18.11.23 POJ 3436 ACM Computer Factory(dinic)

描述 As you know, all the computers used for ACM contests must be identical, so the participants compete on equal terms. That is why all these computers are historically produced at the same factory. Every ACM computer consists of P parts. When all the