Careercup - Facebook面试题 - 6299074475065344
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的更多相关文章
- Careercup - Facebook面试题 - 6026101998485504
2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...
- Careercup - Facebook面试题 - 5344154741637120
2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...
- Careercup - Facebook面试题 - 5765850736885760
2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...
- Careercup - Facebook面试题 - 5733320654585856
2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...
- Careercup - Facebook面试题 - 4892713614835712
2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...
- Careercup - Facebook面试题 - 6321181669982208
2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...
- Careercup - Facebook面试题 - 5177378863054848
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- Careercup - Facebook面试题 - 5435439490007040
2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...
随机推荐
- 跟我一起学习ASP.NET 4.5 MVC4.0(四)(转)
前几个文章中介绍了一些关于MVC4.0的东东,今天我们来看一下登陆验证,也可以说是权限验证,即AuthorizeAttribute.这个可以使用在控制器Controller上,也可以使用在Action ...
- 本地wamp的Internal Server Error错误解决方法
一.本地wamp下调试url重写,加入htaccess文件后提示:500 Internal Server Error...,而删除这个文件网站又可以正常访问,其实就是没有开启url重写的功能.开启一下 ...
- iOS学习笔记--OC系列(1)
前言 从学校毕业进入公司工作已经第3个年头了,回顾这3年的经历,有种迷茫的感觉.在公司我主要是做零售业公司的系统维护,接触的主要是Oracle的Database的东西.但是业务知识和oracle,都没 ...
- Xcode6 模拟器不显示键盘
在学习加法计算器时,程序运行后发现点击模拟器上的输入框时有时候键盘可以弹出来,有时候又弹不出来. 网上查询结果只需要在模拟器的菜单中找到hardware -> keyboard -> 取消 ...
- php连接到数据库操作
<?php $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { ?> 要写的内容代码,比如说Ht ...
- error at ::0 can't find referenced pointcut performance
严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support. ...
- maxlength属性在textarea里奇怪的表现
HTML5给表单带来了很多改变,比如今天要说的maxlength,这个属性可以限制输入框输入的最大字字符数,更方便的是对于粘贴的内容也能够根据字符数自动截断. 最近就接到这要一个需求,限制用户最多输入 ...
- 炫酷实用的jQuery插件 涵盖菜单、按钮、图片
新的一周开始了,今天我们要为大家分享一些全新的jQuery插件和HTML5/CSS3应用,这些jQuery插件不仅非常炫酷,而且还挺实用,这次的分享包含jQuery菜单.CSS3按钮已经多种图片特效, ...
- OpenGL第15,16,17讲小结
这三讲没有什么特别的感觉,15讲把纹理贴到3D字体上,16讲讲了雾的生成,17讲是通过以事先保存好的纹理贴图的字体来显示2D文字,这样子不用调用windows的字体,而是使用纹理中的字体.这样也在一定 ...
- 几道hihocoder不会做的题
1.https://hihocoder.com/problemset/problem/1433?sid=970287 boarding passes,不会做,看的别人的代码,现在还不是很理解. 2. ...