LeetCode - Largest Rectangle in Histogram

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 };

 

转载于:https://www.cnblogs.com/zhuli19901106/p/3568217.html

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值