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 ...
随机推荐
- 根据不同的分辨率选择不同的css文件
<SCRIPT language=javascript> <!-- Begin if (screen.width == 640) { document.write('<link ...
- GridViewWIthSubActivityDemo
GridViewWIthSubActivityDemo 拥有一个sub-activity的GridView示例: 显示一系列图片,点击其中之一的话,在新的sub-activity中放大显示. Imag ...
- 给一个Entity的字段付初始化值(C#)
给一个类去分别赋值,是一个很繁琐切无趣的工作. 那我们就想办法给你一个类去初始化,或许不是一个很效率的方法,但是,从可修改的角度讲,却是一个非常不错的方式. 具体的想法就是,利用类的属性,取出所有 ...
- 英特尔® 实感™ SDK 架构
英特尔® 实感™ SDK在架构上完全不同于其前代系统 — 英特尔® 感知计算 SDK. 如果您使用英特尔感知计算 SDK 进行应用开发,会很快发现,全新的 SDK 可提供增强型编程模式,从而可通过几种 ...
- WIN7实用的运行命令
运行命令主要是DOS操作系统的运行方式.为方便用户的操作,微软公司将一些常用的命令,如DIR,CD等命令全部集成在系统里面:存放这些内部命令的文件是“Command”(文件后缀.com).它与IO.s ...
- js 调用php代码
<?php $test = "var a = ".$_GET['test'].";"; ?> <mce:script type="t ...
- IE6和IE8细节问题
1.对于使用jQuery的ajax.IE6要求使用带有全部的属性:例如IE对下面代码中type:"POST",有严格的要求,如果没有该属性,则无法向后他发送请求 $.ajax({ ...
- JS获取非行间样式及兼容问题
获取非行间样式: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- mfc110ud.dll not found
mfc110ud.dll not found while debugging vs2012 MFC application. Possible Solutions: 1) >>>&g ...
- OC 内存管理机制总结
OC 内存管理机制总结 一:OC内存管理机制目前分为两块,其一自动内存管理机制,其二手动内存管理机制: 1.首先我们从自动内存管理机制讲起: 1)什么是自动内存管理机制,自动内存管理机制就是程序中所创 ...