《Cracking the Coding Interview》——第18章:难题——题目11
2014-04-29 04:30
题目:给定一个由‘0’或者‘1’构成的二维数组,找出一个四条边全部由‘1’构成的正方形(矩形中间可以有‘0’),使得矩形面积最大。
解法:用动态规划思想,记录二维数组每个元素向上下左右四个方向各有多少个连续的‘1’,然后用O(n^3)时间计算出满足条件的最大正方形。时间复杂度O(n^3),空间复杂度O(n^2)。
代码:
// 18.11 Given an NxN matrix of 0s and 1s, find out a subsquare whose all four borders are all 1s. If multiple satisfies the condition, any one is OK.
// I'll return the size and the left top corner of the subsquare.
#include <iostream>
#include <vector>
using namespace std; class Solution {
public:
void maxSubsquare(const vector<vector<int> > &matrix, int &max_left, int &max_top, int &max_size) {
int n = matrix.size(); max_left = max_top = max_size = -; if (n <= ) {
return;
} vector<vector<int> > top (n, vector<int>(n));
vector<vector<int> > bottom(n, vector<int>(n));
vector<vector<int> > left (n, vector<int>(n));
vector<vector<int> > right (n, vector<int>(n)); int i, j;
int tmp; // use DP to preprocess the data, count how many consecutive 1s are there to the left, right, top, bottom of matrix[i][j].
for (i = ; i <= n - ; ++i) {
tmp = ;
for (j = ; j <= n - ; ++j) {
left[i][j] = matrix[i][j] ? (++tmp) : (tmp = );
}
}
for (j = ; j <= n - ; ++j) {
tmp = ;
for (i = ; i <= n - ; ++i) {
top[i][j] = matrix[i][j] ? (++tmp) : (tmp = );
}
}
for (i = n - ; i >= ; --i) {
tmp = ;
for (j = n - ; j >= ; --j) {
right[i][j] = matrix[i][j] ? (++tmp) : (tmp = );
}
}
for (j = n - ; j >= ; --j) {
tmp = ;
for (i = n - ; i >= ; --i) {
bottom[i][j] = matrix[i][j] ? (++tmp) : (tmp = );
}
} int len;
// O(n ^ 3) solution with O(n ^ 2) space usage.
for (i = ; i < n; ++i) {
for (j = ; j < n; ++j) {
for (len = ; len + i <= n && len + j <= n; ++len) {
if (right[i][j] < len || bottom[i][j] < len) {
continue;
}
if (left[i][j + len - ] < len || bottom[i][j + len - ] < len) {
continue;
}
if (right[i + len - ][j] < len || top[i + len - ][j] < len) {
continue;
}
if (left[i + len - ][j + len - ] < len || top[i + len - ][j + len - ] < len) {
continue;
}
// all four borders are '1's.
if (len > max_size) {
max_top = i;
max_left = j;
max_size = len;
}
}
}
} // clear up data
for (i = ; i < n; ++i) {
left[i].clear();
right[i].clear();
top[i].clear();
bottom[i].clear();
}
left.clear();
right.clear();
top.clear();
bottom.clear();
};
}; int main()
{
int n;
int i, j;
vector<vector<int> > matrix;
Solution sol;
int max_left, max_top, max_size; while (cin >> n && n > ) {
matrix.resize(n);
for (i = ; i < n; ++i) {
matrix[i].resize(n);
} for (i = ; i < n; ++i) {
for (j = ; j < n; ++j) {
cin >> matrix[i][j];
}
} sol.maxSubsquare(matrix, max_left, max_top, max_size);
if (max_size > ) {
cout << max_top << ' ' << max_left << endl;
cout << max_top << ' ' << max_left + max_size - << endl;
cout << max_top + max_size - << ' ' << max_left << endl;
cout << max_top + max_size - << ' ' << max_left + max_size - << endl;
} else {
cout << "No subsquare found." << endl;
} for (i = ; i < n; ++i) {
matrix[i].clear();
}
matrix.clear();
} return ;
}
《Cracking the Coding Interview》——第18章:难题——题目11的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- cracking the coding interview系列C#实现
原版内容转自:CTCI面试系列——谷歌面试官经典作品 | 快课网 此系列为C#实现版本 谷歌面试官经典作品(CTCI)目录 1.1 判断一个字符串中的字符是否唯一 1.2 字符串翻转 1.3 去除 ...
- 《Cracking the Coding Interview》——第18章:难题——题目13
2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...
- 《Cracking the Coding Interview》——第18章:难题——题目12
2014-04-29 04:36 题目:最大子数组和的二位扩展:最大子矩阵和. 解法:一个维度上进行枚举,复杂度O(n^2):另一个维度执行最大子数组和算法,复杂度O(n).总体时间复杂度为O(n^3 ...
随机推荐
- ring0 暴力枚举进程
原理:遍历进程ID,然后openprocess,能打开的都枚举出来 ring0 : #include "EnumProcessByForce.h" extern char* PsG ...
- POJ-2481 Cows---树状数组的运用
题目链接: https://vjudge.net/problem/POJ-2481 题目大意: if Si <= Sj and Ej <= Ei and Ei - Si > Ej - ...
- Poj(2236),简单并查集
题目链接:http://poj.org/problem?id=2236 思路很简单,傻逼的我输出写成了FALL,然后遍历的时候for循环写错了,还好很快我就Debug出来了. #include < ...
- 在vue中使用插槽 slot
插槽(slot)这个概念非常重要 插槽的使用场景1:在子组件里面显示父组件的dom <div id='root'> <child content = '<p>Dell&l ...
- 2018.8.3 Java中容易犯错误的问题思考与总结
Java容易犯错误的问题思考 float型 float f = 3.4 是否正确 不正确,应该用强制类型转换.如下所示:float f = (float)3.4 或float f = 3.4f 在ja ...
- 2018.7.26 进程和线程的区别 &&你对 Java平台的理解
进程和线程的区别 1.定义 进程:具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和调度的一个独立单位. 线程:进程的一个实体,是CPU调度和分派的基本单位,它是比进程更 ...
- 2017.11.4 JavaWeb-----基于JavaBean+JSP求任意两数代数和(改进的在JSP页面中无JSP脚本代码的)+网页计数器JavaBean的设计与使用
修改后的JSP中不含有JSP脚本代码这使得JSP程序的清晰性.简单 1.设计JavaBean 的Add.java 类 package beans; public class Add { private ...
- PHP获取当前页面完整路径URL
//PHP获取当前页面完整路径URL 1 <?php function getFullUrl(){ # 解决通用问题 $requestUri = ''; if (isset($_SERVER[' ...
- DevExpress控件经验集合
关于GridControl的可以先看这里:http://blog.csdn.net/dong413876225/article/details/8313094 增加新行,我用了AddNewRow,但是 ...
- ios相关配置
Deployment Target,它控制着运行应用需要的最低操作系统版本.