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的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

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

  7. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  8. cracking the coding interview系列C#实现

    原版内容转自:CTCI面试系列——谷歌面试官经典作品 | 快课网 此系列为C#实现版本 谷歌面试官经典作品(CTCI)目录   1.1 判断一个字符串中的字符是否唯一 1.2 字符串翻转 1.3 去除 ...

  9. 《Cracking the Coding Interview》——第18章:难题——题目13

    2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...

  10. 《Cracking the Coding Interview》——第18章:难题——题目12

    2014-04-29 04:36 题目:最大子数组和的二位扩展:最大子矩阵和. 解法:一个维度上进行枚举,复杂度O(n^2):另一个维度执行最大子数组和算法,复杂度O(n).总体时间复杂度为O(n^3 ...

随机推荐

  1. TP5.1 配置的获取与设置

    我们现在学习对配置文件的获取(Config::get)与设置(Config::set) 我们将学会: (1)获取到一级配置文件 (2)获取到二级配置文件 (3)设置二级配置文件 1.获取一级配置文件 ...

  2. java研发常见问题总结2

    1. String.StringBuffer与StringBuilder之间区别 关于这三个类在字符串处理中的位置不言而喻,那么他们到底有什么优缺点,到底什么时候该用谁呢?下面我们从以下几点说明一下 ...

  3. 请教Nutzwk项目,在beetl页面怎么用shiro标签呢?

    请教Nutzwk项目,在beetl页面怎么用shiro标签呢?  发布于 381天前  作者 WenTao-Love  195 次浏览  复制  上一个帖子  下一个帖子  标签: nutzwk 如题 ...

  4. while counter<10:

    [root@chenbj test]# python Python 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat ...

  5. Thread 创建线程

    1.该线程变量 无参数 我们可以把线程的变量 理解为一个 委托.可以指向一个方法.有点像c语言中的指向函数的指针. 第1步我们创建了 Thread变量t1 ,第2步创建了一个方法threadChild ...

  6. 用js给元素加css

    1.如果是没有CSS文件,或者要修改的不在CSS文件里,那么: document.getElementById('DIV标签的ID').style.属性='属性值'; 这样就可以了.2.如果,样式是写 ...

  7. B3942 Censoring

    爆炸入口 有一个S串和一个T串,长度均小于1,000,000,设当前串为U串,然后从前往后枚举S串一个字符一个字符往U串里添加,若U串后缀为T,则去掉这个后缀继续流程. 这道题确乎是个很好的联系kmp ...

  8. 流形(Manifold)初步

    原文链接 欧几里得几何学(Euclidean Geometry) 两千三百年前,古希腊数学家欧几里得著成了<几何原本>,构建了被后世称为“欧几里得几何学”的研究图形的方法.欧几里得创立了当 ...

  9. eclipse 插件relo使用

    1. eclipse插件安装 在线安装地址:http://relo.csail.mit.edu/update 本地配置,文件下载:http://download.csdn.net/download/s ...

  10. html5 canvas中CanvasGradient对象用法

    html5 中canvas提供了强大的渲染样式,可以实现一些比较复杂的样式设置,今天学习了CanvasGradient对象可以实现一个颜色的渐变 CanvasGradient对象可以实现两种不同形式的 ...