* leetcode #85 in cpp
来源:互联网 发布:淘宝一个好评多少分 编辑:程序博客网 时间:2024/06/05 08:49
Solution:
This question is hard. I did not know how to solve this until I looked up the solution online. Basically to solve this question we employ techniques in #84. That is, we treat this problem as looking for largest rectangle in histogram.
Example:
given
011111101We treat each row as the x-axis, and the 1s as the height of the histogram.For row 1: it is [0, 1,1], max rectangle in this histogram is 2.
For row 2: it is [1,2,2], max rect in this histogram is 4;
For row 3: it is [2,0,3], max rect in this histogram is 3;
Thus we first need to transform the matrix into histograms based on each row(step1), then we use #84 to find out the max rect in the histograms.
Code:
class Solution {public: int maximalRectangle(vector<vector<char>>& matrix) { if(matrix.empty()) return 0; int m = matrix.size(); int n = matrix[0].size(); vector<vector<int>> hist(m, vector<int>(n,0));<span style="white-space:pre"></span>// step 1 for(int i = 0; i < m; i ++){ for(int j = 0; j < n; j ++){ if(matrix[i][j] == '1'){ if(i-1>=0) hist[i][j] = hist[i-1][j] + 1; else hist[i][j] = 1; } } }<span style="white-space:pre"></span> int max_area = 0; for(int i = 0; i < m; i ++){ max_area = max(max_area,largestRectangleArea(hist[i])); } return max_area; } int largestRectangleArea(vector<int>& heights) { heights.push_back(0); stack<int> left_ind; int max_area = 0; int i = 0; while(i < heights.size()){ if(left_ind.empty()) { left_ind.push(i); i++; } else{ if(heights[i] > heights[left_ind.top()]){ left_ind.push(i); i++; }else{ int left = left_ind.top(); left_ind.pop(); max_area = max(max_area, heights[left] *(left_ind.empty()? i :(i - left_ind.top() - 1))); } } } return max_area; } };
0 0
- * leetcode #85 in cpp
- leetcode #12 in cpp
- Leetcode #13 in cpp
- Leetcode #14 in cpp
- leetcode %15 in cpp
- leetcode #16 in cpp
- leetcode #17 in cpp
- leetcode #18 in cpp
- leetcode #20 in cpp
- leetcode #21 in cpp
- leetcode #22 in cpp
- leetcode #23 in cpp
- leetcode #24 in cpp
- leetcode #25 in cpp
- leetcode #26 in cpp
- leetcode #27 in cpp
- leetcode #28 in cpp
- leetcode #29 in cpp
- C#中的深复制和浅复制(在C#中克隆对象)
- 《笑话大全》APP代码详解3
- [leetcode] 214. Shortest Palindrome 解题报告
- Binary Tree Vertical Order Traversal
- LeetCode--No.347--Top K Frequent Elements
- * leetcode #85 in cpp
- Lowest Common Ancestor of a Binary Tree
- LeetCode--No.347--Top K Frequent Elements
- 插入排序
- C语言_推箱子
- 第七届山东省ACM省赛 H Memory Leak 模拟
- Merge Two Sorted List
- LeetCode--No.350--Intersection of Two Arrays II
- android WIFI Socket 文件传输——问题已经解决了,发帖只因高兴--嘿嘿--并不彻底欢迎点评