LeetCode开心刷题三十二天——85. Maximal Rectangle

朴灿烈づ我的快乐病毒、 2023-08-17 15:14 176阅读 0赞
  1. Maximal Rectangle

Hard

161653FavoriteShare

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

Example:

  1. Input:
  2. [
  3. ["1","0","1","0","0"],
  4. ["1","0","1","1","1"],
  5. ["1","1","1","1","1"],
  6. ["1","0","0","1","0"]
  7. ]
  8. Output: 6
  9. IDEA:
  10. https://www.twblogs.net/a/5bc0fa7d2b717711c9241544/zh-cn
  11. The concept of monotone stack(单调栈) was introduced
  12. ONE IMPORTANT THING:
  13. dp[i][j] means the number of "1" in i rows before j columns.
  14. dp[1][2]代表在第一行中的第二列之前1的个数。
  15. #include<stdio.h>
  16. #include<iostream>
  17. #include<string>
  18. #include<vector>
  19. #include<set>
  20. #include<map>
  21. #include<algorithm>
  22. #include<climits>
  23. using namespace std;
  24. class Solution {
  25. public:
  26. int maximalRectangle(vector<vector<char>>& matrix) {
  27. //NULL judge very important
  28. int n1=matrix.size();
  29. if(n1==0) return 0;
  30. int n2=matrix[0].size();
  31. //dp[i][j] is the max number of 1 in columns j rows i
  32. //vector initializer special no need ==
  33. vector<vector<int>> dp(n1,vector<int>(n2));
  34. //initialization
  35. for(int i=0;i<n1;i++)
  36. {
  37. for(int j=0;j<n2;j++)
  38. {
  39. //nested function need to pay attention whether finish all procedure such as this finish one easy to forget the latter ?:
  40. dp[i][j]=(matrix[i][j]=='1')?(j==0?1:dp[i][j-1]+1):0;
  41. }
  42. }
  43. int ans=0;
  44. for(int i=0;i<n1;i++)
  45. {
  46. for(int j=0;j<n2;j++)
  47. {
  48. int len = INT_MAX;
  49. for(int k=i;k<n1;k++)
  50. {
  51. len=min(len,dp[k][j]);
  52. if(len==0) break;
  53. ans=max((k-i+1)*len,ans);
  54. }
  55. }
  56. }
  57. return ans;
  58. }
  59. };
  60. int main()
  61. {
  62. vector<vector<char>> matrix=
  63. {
  64. {
  65. '1','0','1','0','0'},
  66. {
  67. '1','0','1','1','1'},
  68. {
  69. '1','1','1','1','1'},
  70. {
  71. '1','0','0','1','0'}
  72. };
  73. Solution s;
  74. int res=s.maximalRectangle(matrix);
  75. cout<<res<<endl;
  76. return 0;
  77. }
  78. python version:
  79. 1.python version why don't duplicate the C++'s thought,this
  80. method looks more complex.
  81. 2.this version complex to understand but I add some print point help
  82. understand now paste method
  83. class Solution(object):
  84. def maximalRectangle(self, matrix):
  85. """
  86. :type matrix: List[List[str]]
  87. :rtype: int
  88. """
  89. if not matrix or not matrix[0]: return 0
  90. M, N = len(matrix), len(matrix[0])
  91. height = [0] * N
  92. res = 0
  93. for row in matrix:
  94. for i in range(N):
  95. if row[i] == '0':
  96. height[i] = 0
  97. else:
  98. height[i] += 1
  99. print("height1")
  100. print(height)
  101. res = max(res, self.maxRectangleArea(height))
  102. return res
  103. def maxRectangleArea(self, height):
  104. if not height: return 0
  105. print("height")
  106. print(height)
  107. res = 0
  108. stack = list()
  109. print("stack0")
  110. print(stack)
  111. height.append(0)
  112. print("height2")
  113. print(height)
  114. for i in range(len(height)):
  115. cur = height[i]
  116. while stack and cur < height[stack[-1]]:
  117. print("stack")
  118. print(stack)
  119. w = height[stack.pop()]
  120. h = i if not stack else i - stack[-1] - 1
  121. res = max(res, w * h)
  122. stack.append(i)
  123. return res
  124. solu = Solution()
  125. matirx=[
  126. ["1","0","1","0","0"],
  127. ["1","0","1","1","1"],
  128. ["1","1","1","1","1"],
  129. ["1","0","0","1","0"]
  130. ]
  131. print(solu.maximalRectangle(matirx))

转载于:https://www.cnblogs.com/Marigolci/p/11337427.html

发表评论

表情:
评论列表 (有 0 条评论,176人围观)

还没有评论,来说两句吧...

相关阅读