Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.

参考“Largest Rectangle in Histogram”这个题的解法,思想差不多一样,只是用h向量表示Rectangle中此元素中第一行到本行的高度,非常妙的算法:

class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
if(matrix.size() == || matrix[].size() == )
return ;
int cLen = matrix[].size();
int rLen = matrix.size();
//height array
vector<int> h(cLen+,); int max = ; for(int row = ;row<rLen;row++){//for(1)
stack<int> s;
for(int i=;i<cLen+;i++){//for(2)
if(i<cLen){
if(matrix[row][i]=='')
h[i] += ;
else
h[i] = ;
}//end if if(s.empty() || h[s.top()]<=h[i]){
s.push(i);
}else{
while(!s.empty() && h[i]<h[s.top()]){
int top = s.top();
s.pop();
int area = h[top]*(s.empty()?i:(i-s.top()-));
if(area > max)
max = area;
}//end while
s.push(i);
}//end if
}//end for(2)
}//end for(1)
return max;
}//end func
};

[LeetCode] Maximal Rectangle(good)的更多相关文章

  1. leetcode Maximal Rectangle 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...

  2. leetcode之旅(11)-Integer to Roman

    题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

  3. LeetCode之旅(13)-Valid Anagram

    题目介绍: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...

  4. LeetCode高频题目(100)汇总-Java实现

    LeetCode高频题目(100)汇总-Java实现       LeetCode高频题目(100)汇总-Java实现 目录 第01-50题 [Leetcode-easy-1] Two Sum [Le ...

  5. Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)

    Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...

  6. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  7. Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)

    Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...

  8. Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes)

    Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes) 在计算机界中,我们总是追求用有限的资源获取最大的收益. 现在,假设你分别支配着 m 个 0 和 n 个 1. ...

  9. Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner)

    Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端 ...

随机推荐

  1. BZOJ4158 : [POI2007]Railway

    论文题. 随便取个关键点,求出最短路树. 求出所有关键点组成的虚树,将两端都在虚树上的边保留. 对剩下的边求出最小生成树即可得到一组可行解. #include<cstdio> #inclu ...

  2. [Algorithms(Princeton)] Week1 - Percolation

    public class Percolation { private boolean[] openSites; private int gridN; private WeightedQuickUnio ...

  3. java:正则移出html元素

    package com.loongtao.general.crawler.slave; import java.util.regex.Matcher; import java.util.regex.P ...

  4. FileUpload上传图片直接浏览显示(没有上传按钮如何上传)

    1.给FileUpload添加一个onchange事件:FileUpload1.Attributes.Add("onchange", "document.getEleme ...

  5. 斯坦福大学 iOS 开发公开课总结

     斯坦福大学 iOS 开发公开课总结   前言 iPhone 开发相关的教程中最有名的,当数斯坦福大学发布的 "iPhone 开发公开课 " 了.此公开课在以前叫做<iPho ...

  6. WPF之拖动项滚动条自滚动(当拖动项到达高度的边界时候滚动条自己可以上下滚动)

    参考 http://www.cnblogs.com/ListenFly/p/3281997.html Point svPoint = e.GetPosition(sv); if (sv.ActualH ...

  7. MS14-025引起的问题 - 2

    5月,微软在13日发布月度安全更新,其中 有KB2871997和 KB2928120两个知识库文章Knowledgeased(而KB2871997甚至不是Security Bulletin).对于无论 ...

  8. 初学者对Spring MVC的认识

    首先是要一定说明的是,这倒是说明是什么?对吧Spring MVC 是SpringFrameWork的后续产品,并且已经融入到Spring Web Flow中同时Spring MVC 分离了控制器,模型 ...

  9. NV SDK 9.5, 10 and 11

    NVIDIA SDK 10   Overview This all-new collection of DirectX 10 and OpenGL code samples teaches devel ...

  10. Memcached 笔记与总结(2)编译 php-memcache 扩展

    环境:CentOS 6.6 + Apache 2.2.21 + PHP 5.3.10 php-memcache 是 php 写的 memcached 的客户端,以扩展的形式发布. 对于正在运行的 ph ...