《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 ...
随机推荐
- C#中RichTextBox字体不统一(中英文)
this.richTextBox1.Font = new System.Drawing.Font("微软雅黑", 12F);// new System.Drawing.Font(& ...
- 如何用代码的方式取出SAP C4C销售订单创建后所有业务伙伴的数据
比如我创建了一个Sales Order(销售订单)后,如何用代码的方式取出这些通过SAP Partner determination自动填充的Involved Parties信息呢? 一种方法可以使用 ...
- 剑指offer40
class Solution { public: void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) { ) ret ...
- C# unchecked运算符
一.C# unchecked运算符 unchecked运算符用于取消整型算术运算和转换的溢出检查. 二.提示 默认情况下,都是unchecked选项.因此,只有在需要把几个未检查的代码行放在一个明确标 ...
- Vue nodejs商城项目-搭建express框架环境
1.express-project 搭建express框架环境 安装express generator生成器 通过生成器自动创建项目 配置分析 安装 cnpm i -g express-generat ...
- map集合修改其中元素 去除Map集合中所有具有相同值的元素 Properties长久保存的流操作 两种用map记录单词或字母个数的方法
package com.swift.lianxi; import java.util.HashMap; import java.util.Iterator; import java.util.Map; ...
- LeetCode297. Serialize and Deserialize Binary Tree
题目 序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据. 请设计一个算法来实 ...
- BZOJ1965: [Ahoi2005]SHUFFLE 洗牌(exgcd 找规律)
Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 989 Solved: 660[Submit][Status][Discuss] Description ...
- java通过FreeMarker模板生成Excel文件之.ftl模板制作
关于怎么通过freemarker模板生成excel的文章很多,关键点在于怎么制作模板文件.ftl 网上的办法是: (1)把Excel模板的格式调好,另存为xml文件 (2)新建一个.ftl文件,把xm ...
- SpringMVC-实现PUT请求上传文件(转)
因为在图片上传的时候使用的是二进制的方式上传,所以使用隐藏域进行方法转换方式失效,转方法: https://www.cnblogs.com/morethink/p/6378015.html 可是后来我 ...