2014-05-01 01:00

题目链接

原题:

Given a matrix with 's and 0's, a rectangle can be made with 's. What is the maximum area of the rectangle. 

 In this test case the result needs to be . 

How: 

If you see above the 's are used from the first two columns and last four rows making the area or count of 1's to be .

题目:给定一个‘0’、‘1’构成的矩阵,找出其中全部由‘1’构成的子矩阵中面积最大的一个。

解法:这是Leetcode上有的题目,请参考我的题解LeetCode - Maximal Rectangle

代码:

 // http://www.careercup.com/question?id=6299074475065344
#include <vector>
using namespace std; class Solution {
public:
int maxRectangleWithAllOnes(vector<vector<int> > &v) {
n = (int)v.size();
if (n == ) {
return ;
}
m = (int)v[].size();
if (m == ) {
return ;
} int i, j;
int res, max_res; histogram.resize(m);
left.resize(m);
right.resize(m);
fill_n(histogram.begin(), m, );
max_res = ;
for (i = ; i < n; ++i) {
for (j = ; j < m; ++j) {
histogram[j] = v[i][j] ? histogram[j] + v[i][j]: ;
res = maxRectangleInHistogram(histogram);
max_res = res > max_res ? res : max_res;
}
} histogram.clear();
left.clear();
right.clear(); return max_res;
};
private:
vector<int> histogram;
vector<int> left;
vector<int> right;
int n, m; int maxRectangleInHistogram(vector<int> &histogram) {
int i;
int j; left[] = ;
for (i = ; i <= n - ; ++i) {
j = i - ;
left[i] = i;
while (j >= && histogram[i] <= histogram[j]) {
left[i] = left[j];
j = left[j] - ;
}
} right[n - ] = n - ;
for (i = n - ; i >= ; --i) {
j = i + ;
right[i] = i;
while (j <= n - && histogram[i] <= histogram[j]) {
right[i] = right[j];
j = right[j] + ;
}
} int max_res, res;
max_res = ;
for (i = ; i < n; ++i) {
res = histogram[i] * (right[i] - left[i] + );
max_res = res > max_res ? res : max_res;
} return max_res;
};
}; int main()
{
int n, m;
int i, j;
vector<vector<int> > v;
Solution sol; while (scanf("%d%d", &n, &m) == && (n > && m > )) {
v.resize(n);
for (i = ; i < n; ++i) {
v[i].resize(m);
}
for (i = ; i < n; ++i) {
for (j = ; j < m; ++j) {
scanf("%d", &v[i][j]);
}
}
printf("%d\n", sol.maxRectangleWithAllOnes(v)); for (i = ; i < n; ++i) {
v[i].clear();
}
v.clear();
} return ;
}

Careercup - Facebook面试题 - 6299074475065344的更多相关文章

  1. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  2. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  3. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  4. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  5. Careercup - Facebook面试题 - 4892713614835712

    2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...

  6. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  7. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  8. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  9. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

随机推荐

  1. MVC4 使用 ckfinder+ckeditor编辑器

    配置ckfinder for asp.net 版本下载地址  http://cksource.com/ckfinder/downloadckeditor下载地址 http://ckeditor.com ...

  2. Cisco基本命令配置

    实验一 路由器的基本命令操作 1 实验目标 ü 熟悉路由器的命令行操作 ü 能够使用命令行帮助 ü 能够查看路由器接口信息 ü 能够产看路由器配置信息 ü 能够配置以太网接口 ü 能够配置广域网接口 ...

  3. OSPF路由汇总和默认路由设置

    目标 掌握OSPF路由汇总的配置 掌握OSPF默认路由的配置 一.——区域间汇总 配置IP,R2四个环回口 R1(config)#inter s1/0 R1(config-if)#ip add 200 ...

  4. AJAX_1

    AJAX 简介:异步JavaScript 及XML(英文:Asynchronous JavaScript And XML 缩写Ajax).是一种基于 JavaScript和HTTP请求(HTTP re ...

  5. 延迟加载(Lazy Load)

    一个对象,它虽然不包含所需要的所有数据,但是它知道怎么获取这些数据 设计专门的对象来把数据从DB中加载到内存中. 该对象可以完成在加载所需对象的同时,把与之相关的对象也一并加载了. 否则,必须显示加载 ...

  6. JAVA:类,对象,成员属性,成员方法,构造方法,类变量,类方法<2>

    一.类的定义 一个全面的类定义是比较复杂的,  定义如下:

  7. 关于js unshift() 与pop() 功能

    最近在研究如何精简贪吃蛇代码,网上许多大神已经将其精简到30行之内就可以搞定. 我尝试着学习并且研究是否能进一步精简的方式. 偶然间又重新温习了一遍pop()和unshift() 的功能.(之前有学过 ...

  8. 8款超酷体验的jQuery/CSS3应用插件

    1.jQuery/CSS3实现Android Dock菜单效果 这是一款基于jQuery和CSS3的Android Dock菜单效果,点击底部的按钮即可让应用图标浮动上来,并且按钮也出现3D的翻转效果 ...

  9. 6款基于SVG的HTML5应用和动画

    1.HTML5 SVG 3D蝴蝶飞舞动画 逼真超酷 这次我们要分享的这款HTML5动画简直就是逆天,利用SVG制作的3D蝴蝶飞舞动画,蝴蝶飞舞动画非常逼真,蝴蝶飞舞的路线是利用SVG构造的.另外,动画 ...

  10. Ajax 技术二

    一.Ajax与XML案例 例:使用Ajax+XML读取数据表中的分类信息并放入下拉选框中 demo01.php 运行结果: 二.Ajax中的JSON 在Javascript中,可以通过两种方式(XML ...