zoukankan      html  css  js  c++  java
  • Careercup

    2014-05-01 01:00

    题目链接

    原题:

    Given a matrix with 1's and 0's, a rectangle can be made with 1's. What is the maximum area of the rectangle. 
    
    00010 
    11100 
    11110 
    11000 
    11010 In this test case the result needs to be 8. 
    
    How: 
    00010 00010 
    11100 11 100 
    11110 11 110 
    11000 11 000 
    11010 11 010 
    
    If you see above the 11's are used from the first two columns and last four rows making the area or count of 1's to be 8.

    题目:给定一个‘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 == 0) {
     10             return 0;
     11         }
     12         m = (int)v[0].size();
     13         if (m == 0) {
     14             return 0;
     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, 0);
     24         max_res = 0;
     25         for (i = 0; i < n; ++i) {
     26             for (j = 0; j < m; ++j) {
     27                 histogram[j] = v[i][j] ? histogram[j] + v[i][j]: 0;
     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[0] = 0;
     50         for (i = 1; i <= n - 1; ++i) {
     51             j = i - 1;
     52             left[i] = i;
     53             while (j >= 0 && histogram[i] <= histogram[j]) {
     54                 left[i] = left[j];
     55                 j = left[j] - 1;
     56             }
     57         }
     58         
     59         right[n - 1] = n - 1;
     60         for (i = n - 2; i >= 0; --i) {
     61             j = i + 1;
     62             right[i] = i;
     63             while (j <= n - 1 && histogram[i] <= histogram[j]) {
     64                 right[i] = right[j];
     65                 j = right[j] + 1;
     66             }
     67         }
     68         
     69         int max_res, res;
     70         max_res = 0;
     71         for (i = 0; i < n; ++i) {
     72             res = histogram[i] * (right[i] - left[i] + 1);
     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) == 2 && (n > 0 && m > 0)) {
     88         v.resize(n);
     89         for (i = 0; i < n; ++i) {
     90             v[i].resize(m);
     91         }
     92         for (i = 0; i < n; ++i) {
     93             for (j = 0; j < m; ++j) {
     94                 scanf("%d", &v[i][j]);
     95             }
     96         }
     97         printf("%d
    ", sol.maxRectangleWithAllOnes(v));
     98         
     99         for (i = 0; i < n; ++i) {
    100             v[i].clear();
    101         }
    102         v.clear();
    103     }
    104     
    105     return 0;
    106 }
  • 相关阅读:
    基础 ByteBuffer 和 ByteBuf
    Java 堆栈,内存分配理解
    JVM 调优参数设置
    Python 安装 matplotlib 制图
    UOJ#207. 共价大爷游长沙
    ORM学习 一 : JPA JDBC
    常见的Web攻击手段
    《Kubernetes权威指南第2版》学习(二)一个简单的例子
    五 pyJWT使用
    《Kubernetes权威指南第2版》学习(一) Kubernetes是什么
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3702449.html
Copyright © 2011-2022 走看看