2014-05-01 01:00

题目链接

原题:

  1. Given a matrix with 's and 0's, a rectangle can be made with 's. What is the maximum area of the rectangle.
  2.  
  3. In this test case the result needs to be .
  4.  
  5. How:
  6.  
  7. 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

代码:

  1. // http://www.careercup.com/question?id=6299074475065344
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class Solution {
  6. public:
  7. int maxRectangleWithAllOnes(vector<vector<int> > &v) {
  8. n = (int)v.size();
  9. if (n == ) {
  10. return ;
  11. }
  12. m = (int)v[].size();
  13. if (m == ) {
  14. return ;
  15. }
  16.  
  17. int i, j;
  18. int res, max_res;
  19.  
  20. histogram.resize(m);
  21. left.resize(m);
  22. right.resize(m);
  23. fill_n(histogram.begin(), m, );
  24. max_res = ;
  25. for (i = ; i < n; ++i) {
  26. for (j = ; j < m; ++j) {
  27. histogram[j] = v[i][j] ? histogram[j] + v[i][j]: ;
  28. res = maxRectangleInHistogram(histogram);
  29. max_res = res > max_res ? res : max_res;
  30. }
  31. }
  32.  
  33. histogram.clear();
  34. left.clear();
  35. right.clear();
  36.  
  37. return max_res;
  38. };
  39. private:
  40. vector<int> histogram;
  41. vector<int> left;
  42. vector<int> right;
  43. int n, m;
  44.  
  45. int maxRectangleInHistogram(vector<int> &histogram) {
  46. int i;
  47. int j;
  48.  
  49. left[] = ;
  50. for (i = ; i <= n - ; ++i) {
  51. j = i - ;
  52. left[i] = i;
  53. while (j >= && histogram[i] <= histogram[j]) {
  54. left[i] = left[j];
  55. j = left[j] - ;
  56. }
  57. }
  58.  
  59. right[n - ] = n - ;
  60. for (i = n - ; i >= ; --i) {
  61. j = i + ;
  62. right[i] = i;
  63. while (j <= n - && histogram[i] <= histogram[j]) {
  64. right[i] = right[j];
  65. j = right[j] + ;
  66. }
  67. }
  68.  
  69. int max_res, res;
  70. max_res = ;
  71. for (i = ; i < n; ++i) {
  72. res = histogram[i] * (right[i] - left[i] + );
  73. max_res = res > max_res ? res : max_res;
  74. }
  75.  
  76. return max_res;
  77. };
  78. };
  79.  
  80. int main()
  81. {
  82. int n, m;
  83. int i, j;
  84. vector<vector<int> > v;
  85. Solution sol;
  86.  
  87. while (scanf("%d%d", &n, &m) == && (n > && m > )) {
  88. v.resize(n);
  89. for (i = ; i < n; ++i) {
  90. v[i].resize(m);
  91. }
  92. for (i = ; i < n; ++i) {
  93. for (j = ; j < m; ++j) {
  94. scanf("%d", &v[i][j]);
  95. }
  96. }
  97. printf("%d\n", sol.maxRectangleWithAllOnes(v));
  98.  
  99. for (i = ; i < n; ++i) {
  100. v[i].clear();
  101. }
  102. v.clear();
  103. }
  104.  
  105. return ;
  106. }

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. 跟我一起学习ASP.NET 4.5 MVC4.0(四)(转)

    前几个文章中介绍了一些关于MVC4.0的东东,今天我们来看一下登陆验证,也可以说是权限验证,即AuthorizeAttribute.这个可以使用在控制器Controller上,也可以使用在Action ...

  2. 本地wamp的Internal Server Error错误解决方法

    一.本地wamp下调试url重写,加入htaccess文件后提示:500 Internal Server Error...,而删除这个文件网站又可以正常访问,其实就是没有开启url重写的功能.开启一下 ...

  3. iOS学习笔记--OC系列(1)

    前言 从学校毕业进入公司工作已经第3个年头了,回顾这3年的经历,有种迷茫的感觉.在公司我主要是做零售业公司的系统维护,接触的主要是Oracle的Database的东西.但是业务知识和oracle,都没 ...

  4. Xcode6 模拟器不显示键盘

    在学习加法计算器时,程序运行后发现点击模拟器上的输入框时有时候键盘可以弹出来,有时候又弹不出来. 网上查询结果只需要在模拟器的菜单中找到hardware -> keyboard -> 取消 ...

  5. php连接到数据库操作

    <?php $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { ?> 要写的内容代码,比如说Ht ...

  6. error at ::0 can't find referenced pointcut performance

    严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support. ...

  7. maxlength属性在textarea里奇怪的表现

    HTML5给表单带来了很多改变,比如今天要说的maxlength,这个属性可以限制输入框输入的最大字字符数,更方便的是对于粘贴的内容也能够根据字符数自动截断. 最近就接到这要一个需求,限制用户最多输入 ...

  8. 炫酷实用的jQuery插件 涵盖菜单、按钮、图片

    新的一周开始了,今天我们要为大家分享一些全新的jQuery插件和HTML5/CSS3应用,这些jQuery插件不仅非常炫酷,而且还挺实用,这次的分享包含jQuery菜单.CSS3按钮已经多种图片特效, ...

  9. OpenGL第15,16,17讲小结

    这三讲没有什么特别的感觉,15讲把纹理贴到3D字体上,16讲讲了雾的生成,17讲是通过以事先保存好的纹理贴图的字体来显示2D文字,这样子不用调用windows的字体,而是使用纹理中的字体.这样也在一定 ...

  10. 几道hihocoder不会做的题

    1.https://hihocoder.com/problemset/problem/1433?sid=970287 boarding passes,不会做,看的别人的代码,现在还不是很理解. 2.  ...