[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 ...
随机推荐
- Linux gnome
一.主题风格网站:gnome-look.org.deviantart.com.Linux公社 我使用的主题是:http://gnome-look.org/content/show.php/OS+X+1 ...
- barabasilab-networkScience学习笔记3-随机网络模型
第一次接触复杂性科学是在一本叫think complexity的书上,Allen博士很好的讲述了数据结构与复杂性科学,barabasi是一个知名的复杂性网络科学家,barabasilab则是他所主导的 ...
- WINDOWS页式内存管理解析
jpg 改 rar
- C#分布式缓存Couchbase使用
目前C#业界使用得最多的 Cache 系统主要是 Memcached和 Redis. 这两个 Cache 系统可以说是比较成熟的解决方案,也是很多系统当然的选择. 一.简介 目前C#业界使用得最多的 ...
- Loadrunner在场景中添加多个负载机报错:Action.c(38): Error -26488: Could not obtain information about submitted解决方法
Error -26488: Could not obtain information about submitted file "E:\.jpg": _stat32 rc=-1, ...
- Loadrunne实现多个场景运行
如何让Loadrunne实现多个场景运行? 场景分析: 有3个不同的场景,分别为搜索,下载,上传,其中3个场景执行顺序为按照搜索->下载->上传流程操作:哪么如何让Loadrunner中如 ...
- 用val()获取与设置input的值
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- light oj 1422 Halloween Costumes (区间dp)
题目链接:http://vjudge.net/contest/141291#problem/D 题意:有n个地方,每个地方要穿一种衣服,衣服可以嵌套穿,一旦脱下的衣服不能再穿,除非穿同样的一件新的,问 ...
- AngularJS 包含HTML文件
类似于python tornado的include方法,同样是可以在一个html文件中加载另外一个html文件,这样可以不用重复的写一些几乎不改变的代码. 首先创建两个文件,然后代码如下: <! ...
- jquery笔记(操作HTML)
获取: $("selector").text(): 获取元素里面的文本 $("selector").html(): 获取元素里面的代码标签 $("se ...