LeetCode(85):最大矩形


Hard!

題目描述:

給定一個僅包含 0 和 1 的二維二進制矩陣,找出只包含 1 的最大矩形,並返回其面積。

示例:

輸入:
[
  ["1","0","1","0","0"],
  ["1","0","1","1","1"],
  ["1","1","1","1","1"],
  ["1","0","0","1","0"]
]
輸出: 6

解題思路:

此題是之前那道的 Largest Rectangle in Histogram 直方圖中最大的矩形 (http://www.cnblogs.com/grandyang/p/4322653.html)的擴展,這道題的二維矩陣每一層向上都可以看做一個直方圖,輸入矩陣有多少行,就可以形成多少個直方圖,對每個直方圖都調用http://www.cnblogs.com/grandyang/p/4322653.html中的方法,就可以得到最大的矩形面積。那么這道題唯一要做的就是將每一層構成直方圖,由於題目限定了輸入矩陣的字符只有 '0' 和 '1' 兩種,所以處理起來也相對簡單。方法是,對於每一個點,如果是‘0’,則賦0,如果是 ‘1’,就賦 之前的height值加上1。

C++解法一:

 1 class Solution {
 2 public:
 3     int maximalRectangle(vector<vector<char> > &matrix) {
 4         int res = 0;
 5         vector<int> height;
 6         for (int i = 0; i < matrix.size(); ++i) {
 7             height.resize(matrix[i].size());
 8             for (int j = 0; j < matrix[i].size(); ++j) {
 9                 height[j] = matrix[i][j] == '0' ? 0 : (1 + height[j]);
10             }
11             res = max(res, largestRectangleArea(height));
12         }
13         return res;
14     }
15     int largestRectangleArea(vector<int> &height) {
16         int res = 0;
17         stack<int> s;
18         height.push_back(0);
19         for (int i = 0; i < height.size(); ++i) {
20             if (s.empty() || height[s.top()] <= height[i]) s.push(i);
21             else {
22                 int tmp = s.top();
23                 s.pop();
24                 res = max(res, height[tmp] * (s.empty() ? i : (i - s.top() - 1)));
25                 --i;
26             }
27         }
28         return res;
29     }
30 };

我們也可以在一個函數內完成,這樣代碼看起來更加簡潔一些:

C++解法二:

 1 class Solution {
 2 public:
 3     int maximalRectangle(vector<vector<char>>& matrix) {
 4         if (matrix.empty() || matrix[0].empty()) return 0;
 5         int res = 0, m = matrix.size(), n = matrix[0].size();
 6         vector<int> height(n + 1, 0);
 7         for (int i = 0; i < m; ++i) {
 8             stack<int> s;
 9             for (int j = 0; j < n + 1; ++j) {
10                 if (j < n) {
11                     height[j] = matrix[i][j] == '1' ? height[j] + 1 : 0;
12                 }
13                 while (!s.empty() && height[s.top()] >= height[j]) {
14                     int cur = s.top(); s.pop();
15                     res = max(res, height[cur] * (s.empty() ? j : (j - s.top() - 1)));
16                 }
17                 s.push(j);
18             }
19         }
20         return res;
21     }
22 };

下面這種方法的思路很巧妙,height數組和上面一樣,這里的left數組表示左邊界是1的位置,right數組表示右邊界是1的位置,那么對於任意一行的第j個位置,矩形為(right[j] - left[j]) * height[j],我們舉個例子來說明,比如給定矩陣為:

[
  [1, 1, 0, 0, 1],
  [0, 1, 0, 0, 1],
  [0, 0, 1, 1, 1],
  [0, 0, 1, 1, 1],
  [0, 0, 0, 0, 1]
]

第0行:

h: 1 1 0 0 1
l: 0 0 0 0 4
r: 2 2 5 5 5 

第1行:

h: 1 1 0 0 1
l: 0 0 0 0 4
r: 2 2 5 5 5 

第2行:

h: 0 0 1 1 3
l: 0 0 2 2 4
r: 5 5 5 5 5

第3行:

h: 0 0 2 2 4
l: 0 0 2 2 4
r: 5 5 5 5 5

第4行:

h: 0 0 0 0 5
l: 0 0 0 0 4
r: 5 5 5 5 5 

C++解法三:

 1 class Solution {
 2 public:
 3     int maximalRectangle(vector<vector<char>>& matrix) {
 4         if (matrix.empty() || matrix[0].empty()) return 0;
 5         int res = 0, m = matrix.size(), n = matrix[0].size();
 6         vector<int> height(n, 0), left(n, 0), right(n, n);
 7         for (int i = 0; i < m; ++i) {
 8             int cur_left = 0, cur_right = n;
 9             for (int j = 0; j < n; ++j) {
10                 if (matrix[i][j] == '1') ++height[j];
11                 else height[j] = 0;
12             }
13             for (int j = 0; j < n; ++j) {
14                 if (matrix[i][j] == '1') left[j] = max(left[j], cur_left);
15                 else {left[j] = 0; cur_left = j + 1;}
16             }
17             for (int j = n - 1; j >= 0; --j) {
18                 if (matrix[i][j] == '1') right[j] = min(right[j], cur_right);
19                 else {right[j] = n; cur_right = j;}
20             }
21             for (int j = 0; j < n; ++j) {
22                 res = max(res, (right[j] - left[j]) * height[j]);
23             }
24         }
25         return res;
26     }
27 };

我們也可以通過合並一些for循環,使得運算速度更快一些:

C++解法四:

 1 class Solution {
 2 public:
 3     int maximalRectangle(vector<vector<char>>& matrix) {
 4         if (matrix.empty() || matrix[0].empty()) return 0;
 5         int res = 0, m = matrix.size(), n = matrix[0].size();
 6         vector<int> height(n, 0), left(n, 0), right(n, n);
 7         for (int i = 0; i < m; ++i) {
 8             int cur_left = 0, cur_right = n;
 9             for (int j = 0; j < n; ++j) {
10                 if (matrix[i][j] == '1') {
11                     ++height[j];
12                     left[j] = max(left[j], cur_left);
13                 } else {
14                     height[j] = 0;
15                     left[j] = 0;
16                     cur_left = j + 1;
17                 }
18             }
19             for (int j = n - 1; j >= 0; --j) {
20                 if (matrix[i][j] == '1') {
21                     right[j] = min(right[j], cur_right);
22                 } else {
23                     right[j] = n;
24                     cur_right = j;
25                 }
26                 res = max(res, (right[j] - left[j]) * height[j]);
27             }
28         }
29         return res;
30     }
31 };

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM