LeetCode开心刷题三十四天——84. Largest Rectangle in Histogram

喜欢ヅ旅行 2023-08-17 15:14 152阅读 0赞
  1. Largest Rectangle in Histogram

Hard

215858FavoriteShare

Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.

histogram.png
Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].

histogram_area.png
The largest rectangle is shown in the shaded area, which has area = 10 unit.

Example:

  1. Input: [2,1,5,6,2,3]
  2. Output: 10

This problem is a bold attempt(大胆尝试) for me ,because I try multiply ways to solve this problem.I’ve been heard multi-way can make people get clear

左右遍历法:

1.O(n) 左右遍历

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<string>
  4. #include<vector>
  5. #include<set>
  6. #include<map>
  7. #include<algorithm>
  8. #include<stack>
  9. #include<climits>
  10. //
  11. using namespace std;
  12. class Solution
  13. {
  14. public:
  15. int largestRectangleArea(vector<int>& height)
  16. {
  17. //NULL
  18. if(height.empty()) return 0;
  19. //Judge
  20. int n=height.size();
  21. //store
  22. int *left=new int[n];
  23. int *right=new int[n];
  24. int maxArea=0;
  25. //left
  26. left[0]=1;
  27. for(int i=1;i<n;i++)
  28. {
  29. if(height[i]>height[i-1])
  30. {
  31. left[i]=1;
  32. }
  33. else
  34. {
  35. int j=i-1;
  36.           //always easy miss =
  37. while(j>=0&&height[j]>=height[i])
  38. {
  39. j-=left[j];
  40. }
  41. left[i]=i-j;
  42. //cout<<"i"<<left[i]<<endl;
  43. }
  44. }
  45. //right
  46. right[n-1]=1;
  47. for(int i=n-2;i>=0;i--)
  48. {
  49. if(height[i]>height[i+1])
  50. {
  51. right[i]=1;
  52. }
  53. else
  54. {
  55. int j=i+1;
  56. while(j<n&&height[j]>=height[i])
  57. {
  58. j+=right[j];
  59. }
  60. right[i]=j-i;
  61. }
  62. }
  63. //MaxArea
  64. for(int i=0;i<n;i++)
  65. {
  66. //former problem cannot use right+left,1.left is not abs 2.i is not 0
  67. maxArea=max(maxArea,(right[i]-left[i]+1)*height[i]);
  68. cout<<"r"<<right[i]<<"l"<<left[i]<<endl;
  69. cout<<"M"<<maxArea<<endl;
  70. }
  71. return maxArea;
  72. }
  73. };
  74. int main()
  75. {
  76. vector<int> res={
  77. 1,1};
  78. Solution s;
  79. cout<<s.largestRectangleArea(res)<<endl;
  80. return 0;
  81. }

2.O(n^2)

省代码但是耗时

  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<string>
  4. #include<vector>
  5. #include<set>
  6. #include<map>
  7. #include<algorithm>
  8. #include<stack>
  9. #include<climits>
  10. //
  11. using namespace std;
  12. //similar to the idea of scrolling array
  13. class Solution
  14. {
  15. public:
  16. int largestRectangleArea(vector<int>& height)
  17. {
  18. //NULL
  19. if(height.empty()) return 0;
  20. //initialize
  21. //Judge
  22. int n=height.size();
  23. //store
  24. int minH=INT_MAX;
  25. int maxArea=0;
  26. //main part
  27. //forward
  28. for(int i=0;i<n;i++)
  29. {
  30. if(i+1<n&&height[i]<=height[i+1])
  31. {
  32. continue;
  33. }
  34. minH=INT_MAX;
  35. //backward
  36. for(int j=i;j>=0;j--)
  37. {
  38. minH=min(minH,height[j]);
  39. cout<<"mH "<<minH<<" h[j] "<<height[j]<<endl;
  40. int area=(i-j+1)*minH;
  41. maxArea=max(maxArea,area);
  42. cout<<" minH "<<minH<<" i "<<i<<" j "<<j<<" maxArea "<<maxArea<<endl;
  43. }
  44. }
  45. //return
  46. return maxArea;
  47. }
  48. };
  49. int main()
  50. {
  51. vector<int> res={
  52. 2,1,5,6,2,3};
  53. Solution s;
  54. cout<<s.largestRectangleArea(res)<<endl;
  55. return 0;
  56. }

单调栈:

C++:

  1. class Solution {
  2. public:
  3. int largestRectangleArea(vector<int> &height) {
  4. int res = 0;
  5. for (int i = 0; i < height.size(); ++i) {
  6. if (i + 1 < height.size() && height[i] <= height[i + 1]) {
  7. continue;
  8. }
  9. int minH = height[i];
  10. for (int j = i; j >= 0; --j) {
  11. minH = min(minH, height[j]);
  12. int area = minH * (i - j + 1);
  13. res = max(res, area);
  14. }
  15. }
  16. return res;
  17. }
  18. };

python:

  1. class Solution(object):
  2. def largestRectangleArea(self, heights):
  3. """
  4. :type heights: List[int]
  5. :rtype: int
  6. """
  7. # store
  8. stack = list()
  9. res = 0
  10. heights.append(0)
  11. # judge
  12. N = len(heights)
  13. for i in range(N):
  14. if not stack or heights[i] > heights[stack[-1]]:
  15. stack.append(i)
  16. else:
  17. while stack and heights[i] <= heights[stack[-1]]:
  18. h = heights[stack[-1]]
  19. # pop to find boundary
  20. stack.pop()
  21. # special minus distance i distance+1 stack[-1] distance-1
  22. w = i if not stack else i - stack[-1] - 1
  23. res = max(res, h * w)
  24. # this step easy forget.i isn't former calculation.Need to push stack
  25. stack.append(i)
  26. return res
  27. solu=Solution()
  28. # 赋值输出一条龙
  29. print(solu.largestRectangleArea(heights=[2,1,5,6,2,3]))

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

发表评论

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

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

相关阅读