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. 洛谷 P2251 质量检测

    题目背景 无 题目描述 为了检测生产流水线上总共N件产品的质量,我们首先给每一件产品打一个分数A表示其品质,然后统计前M件产品中质量最差的产品的分值Q[m] = min{A1, A2, ... Am} ...

  2. Zabbix3.0部署实践

    Zabbix3.0部署实践   Zabbix3整个web界面做了一个全新的设计. 1.1Zabbix环境准备 [root@linux-node1 ~]# cat /etc/redhat-release ...

  3. PHP编译安装时常见错误及解决办法,大全

    1.   configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution ...

  4. PHPmailer群发Gmail的常见问题

    博主小白一枚,phpmailer只会一些基本的用法,就这样一个邮件的群发功能也难住了我一周,下面把我遇到的问题给大家总结一下 1.Could not authenticate 首先,如果你没有使用循环 ...

  5. 彩色图像直方图均衡(Histogram Equalization)

    直方图均衡(Histogram Equalization) 一般步骤: 1.统计直方图每个灰度级出现的次数(概率) 2.累计归一化的直方图 3.计算新的像素值 重要:彩色直方图均衡不能对RGB分别做再 ...

  6. Oracle口令文件管理

    Oracle的口令文件目录 $ORACLE_HOME/dbs/orapw$ORACLE_SID 建立口令文件 orapwd file=$ORACLE_HOME/dba/orapw$ORACLE_SID ...

  7. linux 安装 zookeeper

    第一步 下载 zookeeper: http://archive.apache.org/dist/zookeeper/ 第二步 解压: tar -xzvf zookeeper-3.4.5.tar.gz ...

  8. springMVC入门一

    一.准备工作 eclipse使用maven搭建项目,项目名称为HelloSpringMVC 二.搭建好的项目如下: 项目介绍:实现简单的helloworld 三.具体代码 controller类:He ...

  9. JAVA / MySql 编程——第七章 JDBC

    1.JDBC:JDBA是Java数据库连接(Java DataBase Connectivity)技术的简称,提供连接各种常用数据库的能力:         ●Java是通过JDBC技术实现对各种数据 ...

  10. 【Effective C++ 读书笔记】条款03: 尽量使用 const

    关键字const多才多艺,变化多端却不高深莫测. const 修饰指针 面对指针, 你可以指出 指针自身.指针所指物.或者两者都不是 const. 如果关键字 const 出现在星号左边,表示被指物是 ...