[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 = ; j < size; ++j) {
if (matrix[row][col + j] == ) return false;
if (matrix[row + size - ][col + j] == ) return false;
}
for (int i = ; i < size - ; ++i) {
if (matrix[row + i][col] == ) return false;
if (matrix[row + i][col + size - ] == ) return false;
}
return true;
} Subsquare* find_square_with_size(vector<vector<int>> &matrix, int squareSize) {
int cnt = matrix.size() - squareSize + ;
for (int row = ; row < cnt; ++row) {
for (int col = ; 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 >= ; --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 = , zerosBelow = ;
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 - ];
SquareCell *bottomRight = matrix[row + size - ][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() - ; r >= ; --r) {
for (int c = matrix.size() - ; c >= ; --c) {
int rightZeros = , belowZeros = ;
if (matrix[r][c] == ) {
++rightZeros;
++belowZeros;
if (c + < matrix.size()) {
SquareCell *pre = res[r][c + ];
rightZeros += pre->zerosRight;
}
if (r + < matrix.size()) {
SquareCell *pre = res[r + ][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 + ;
for (int row = ; row < cnt; ++row) {
for (int col = ; 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 >= ; --i) {
Subsquare *square = find_square_with_size(processed, i);
if (square) return square;
}
return NULL;
}
[CareerCup] 18.11 Maximum Subsquare 最大子方形的更多相关文章
- 日本IT行业劳动力缺口达22万 在日中国留学生迎来就业好时机 2017/07/18 11:25:09
作者:倪亚敏 来源:日本新华侨报 发布时间:2017/07/18 11:25:09 据日本政府提供的数据,日本2018年应届毕业生的“求人倍率”已经达到了1.78倍.换言之,就是100名大学生 ...
- [18/11/11] java标识符及变量
一.标识符规范 1.必须以字母.下划线 .美元符号开头. 即数字不能作为开头,其它位随便 2.不可以是java关键字(即保留字), 如static .class.new 等 . 注:int 年 ...
- [CareerCup] 18.12 Largest Sum Submatrix 和最大的子矩阵
18.12 Given an NxN matrix of positive and negative integers, write code to find the submatrix with t ...
- [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出现的个数,比如如 ...
- highgui.h备查 分类: C/C++ OpenCV 2014-11-08 18:11 292人阅读 评论(0) 收藏
/*M/////////////////////////////////////////////////////////////////////////////////////// // // IMP ...
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [CareerCup] 18.1 Add Two Numbers 两数相加
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...
- [CareerCup] 18.10 Word Transform 单词转换
18.10 Given two words of equal length that are in a dictionary, write a method to transform one word ...
- [CareerCup] 18.9 Find and Maintain the Median Value 寻找和维护中位数
18.9 Numbers are randomly generated and passed to a method. Write a program to find and maintain the ...
随机推荐
- mysql无法远程连接的解决方法
在阿里云服务器上安装好MySQL后,首先想到的就是安装一款工具来管理数据库,一开始选择了phpMyAdmin,这个工具安装很简单,只要解压到能访问的目录下就行了.在浏览器中访问phpMyAdm ...
- C# 指针操作图像 细化处理
/// <summary> /// 图形细化 /// </summary> /// <param name="srcImg"></para ...
- IplImage, CvMat, Mat 的关系
IplImage, CvMat, Mat 的关系 转载来源:http://www.cnblogs.com/summerRQ/articles/2406109.html opencv中常见的与图像操作有 ...
- Comet:基于 HTTP 长连接的“服务器推”技术解析
原文链接:http://www.cnblogs.com/deepleo/p/Comet.html 一.背景介绍 传统web请求,是显式的向服务器发送http Request,拿到Response后显示 ...
- Redis持久化实践及灾难恢复模拟
参考资料: Redis Persistence http://redis.io/topics/persistence Google Groups https://groups.google.com/f ...
- 在Activity中响应ListView内部按钮的点击事件
最近交流群里面有人问到一个问题:如何在Activity中响应ListView内部按钮的点击事件,不要在Adapter中响应? 对于这个问题,我最初给他的解答是,在Adapter中定义一个回调接口,在A ...
- express随记01
系统变量的设置 app.get(env) | process.env.NODE_ENV: 会自动判断当前环境类型; app.get(port) | process.env.PORT: 必须手动设置; ...
- express-15 与生产相关的问题
执行环境 Express支持执行环境的概念,它是一种在生产.开发或测试模式中运行应用程序的方法.实际上你可以按自己的想法创建很多种不同的环境. 要记住,开发.生产和测试是"标准"环 ...
- 读取接口XML和批量导入数据SqlBulkCopy
首先是C#处理xml文档 string urlStr = string.Format("http://……?timeBeg={0}&timeEnd={1}", timeBe ...
- UVALive6900 Road Repair(树的点分治)
题目大概说一棵树,树边有费用和收益两个属性,求一条收益和最大的路径满足费用和不超过C. 树上任意两点的路径都可以看成是过某一个子树根的路径,显然树分治. 治的时候要解决的一个问题是,找到费用小于等于某 ...