随笔- 509  文章- 0  评论- 151  阅读- 22万 

Largest Rectangle in Histogram

2014.2.26 05:06

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.

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

 

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

For example,
Given height = [2,1,5,6,2,3],
return 10.

Solution1:

  In this problem, you'll find the largest rectangle that is completely contained in the histogram.

  The brute-force solution is O(n^2). For each grid, you scan them all through to determine the left and right borders, calculate the area and update the result. Surely it won't get you an acceptable outcome.

  In the brute-force solution, we start from the current grid and scan in both directions one by one. If not one by one, it would be more efficient. The solution below will use an extra array to do dymamic programming. The very few differences in the while loop is the key to this efficient DP solution.

  Total time and space complexities are both O(n).

Accepted code:

复制代码
 1 // 3TLE, 1AC, DP solution
 2 class Solution {
 3 public:
 4     int largestRectangleArea(vector<int> &height) {
 5         int i;
 6         int n;
 7         
 8         n = (int)height.size();
 9         if (n == 0) {
10             return 0;
11         }
12         
13         vector<int> ll, rr;
14         int max_area = 0;
15         int area = 0;
16         
17         ll.resize(n);
18         rr.resize(n);
19         for (i = 0; i < n; ++i) {
20             ll[i] = i;
21             rr[i] = i;
22         }
23         for (i = 0; i <= n - 1; ++i) {
24             while (ll[i] - 1 >= 0 && height[ll[i] - 1] >= height[i]) {
25                 ll[i] = ll[ll[i] - 1];
26             }
27         }
28         for (i = n - 1; i >= 0; --i) {
29             while (rr[i] + 1 <= n - 1 && height[rr[i] + 1] >= height[i]) {
30                 rr[i] = rr[rr[i] + 1];
31             }
32         }
33         
34         for (i = 0; i < n; ++i) {
35             area = (rr[i] - ll[i] + 1) * height[i];
36             if (area > max_area) {
37                 max_area = area;
38             }
39         }
40         
41         ll.clear();
42         rr.clear();
43         return max_area;
44     }
45 };
复制代码

Solution2:

  This solution is quite difficult to figure out. Though I would say I can understand how it works, I'd say the original author of this algorithm is just magician to be so creative.

  Here is where my reference came from, for your information: Largest Rectangular Area in Histogram.

  Although the time and space are on the same level, but the constant coefficients are better indeed.

  This algorithm is better than the last one, but I'd still prefer the last one because it's easier to write and understand, as well as efficient enough to solve the problem at hand. Tricky code is a double-edged sword, use it wisely.

Accepted code:

复制代码
 1 // 1WA, 1AC, the more optimized O(n) solution with stack.
 2 #include <stack>
 3 #include <vector>
 4 using namespace std;
 5 
 6 class Solution {
 7 public:
 8     int largestRectangleArea(vector<int> &height) {
 9         int i;
10         int n;
11         
12         n = (int)height.size();
13         if (n == 0) {
14             return 0;
15         }
16         
17         int max_area = 0;
18         int area;
19         stack<int> st;
20         int top;
21         
22         for (i = 0; i < n; ++i) {
23             if (st.empty() || height[st.top()] <= height[i]) {
24                 st.push(i);
25             } else {
26                 while (!st.empty() && height[st.top()] > height[i]) {
27                     top = st.top();
28                     st.pop();
29                     if (st.empty()) {
30                         area = i * height[top];
31                     } else {
32                         area = (i - st.top() - 1) * height[top];
33                     }
34                     if (area > max_area) {
35                         max_area = area;
36                     }
37                 }
38                 st.push(i);
39             }
40         }
41         while (!st.empty()) {
42             top = st.top();
43             st.pop();
44             if (st.empty()) {
45                 area = i * height[top];
46             } else {
47                 area = (i - st.top() - 1) * height[top];
48             }
49             if (area > max_area) {
50                 max_area = area;
51             }
52         }
53         
54         return max_area;
55     }
56 };
复制代码

 

 posted on   zhuli19901106  阅读(283)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示