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 ...
随机推荐
- (转)Android面试题
1. 下列哪些语句关于内存回收的说明是正确的? (b ) A. 程序员必须创建一个线程来释放内存 B.内存回收程序负责释放无用内存 C.内存回收程序允许程序员直接释放内存 D.内存回收程序可以在 ...
- cmd 命令收集
window类 1.命令打开系统设置页面 1.control keymgr.dll 打开凭据管理器 2.gpedit.msc 打开管理面板 3.mspaint--------画图板 4.msts ...
- C# 序列化(二)二进制序列化的案例
这篇是针对上一篇讲序列化的文章的一个实际案例,WinForm程序的主界面如下:
- java开发命名规范(转载)
java开发命名规范 使用前注意事项: 1. 由于Java面向对象编程的特性, 在命名时应尽量选择名词 2. 驼峰命名法(Camel-Case): 当变量名或函式名是由一个或多个单字连结在一起,而 ...
- iOS - 网络语线程(OC)
1. 检测网络状态 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the vi ...
- C#中多线程的简单应用
下面是C#中使用多线程的一个简单用法介绍: //主线程: Thread thread = new Thread(new ThreadStart(ReadExportData));//创建分支线程thr ...
- WCF之并发,吞吐量和限流
并发 Single重入模式.对于每一个服务实例,同一时刻只能处理一个请求,其他对该实例的请求被排队. PerCall,每一线程会分配一个新的服务实例上.不会有并发性问题.不影响吞吐量. PerSess ...
- Extjs Cmd 学习笔记
1.sencha app build 命令 <!-- <x-compile> --> <!-- <x-bootstrap> ...
- js 函数的调用模式
1.函数调用 调用一个函数将暂停当前函数的执行,传递控制权和参数给新函数.除了函数声明时定义的形参,每个函数还接受两个附加的参数:this和arguments(arguments并不是一个真正的数组, ...
- java虚拟机理解探索1
以下内容源于个人对<深入java虚拟机>的理解总结 基本概念: java虚拟机可以指一种抽象规范,也可以指一种具体实现,亦可以指一个java虚拟机实例. 虚拟机生命周期: 一个java虚拟 ...