LeetCode - Largest Rectangle in Histogram
2014-02-26 05:22
369 查看
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 =
The largest rectangle is shown in the shaded area, which has area =
For example,
Given height =
return
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:
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:
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 =
10unit.
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:
// 3TLE, 1AC, DP solution class Solution { public: int largestRectangleArea(vector<int> &height) { int i; int n; n = (int)height.size(); if (n == 0) { return 0; } vector<int> ll, rr; int max_area = 0; int area = 0; ll.resize(n); rr.resize(n); for (i = 0; i < n; ++i) { ll[i] = i; rr[i] = i; } for (i = 0; i <= n - 1; ++i) { while (ll[i] - 1 >= 0 && height[ll[i] - 1] >= height[i]) { ll[i] = ll[ll[i] - 1]; } } for (i = n - 1; i >= 0; --i) { while (rr[i] + 1 <= n - 1 && height[rr[i] + 1] >= height[i]) { rr[i] = rr[rr[i] + 1]; } } for (i = 0; i < n; ++i) { area = (rr[i] - ll[i] + 1) * height[i]; if (area > max_area) { max_area = area; } } ll.clear(); rr.clear(); return max_area; } };
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:
// 1WA, 1AC, the more optimized O(n) solution with stack. #include <stack> #include <vector> using namespace std; class Solution { public: int largestRectangleArea(vector<int> &height) { int i; int n; n = (int)height.size(); if (n == 0) { return 0; } int max_area = 0; int area; stack<int> st; int top; for (i = 0; i < n; ++i) { if (st.empty() || height[st.top()] <= height[i]) { st.push(i); } else { while (!st.empty() && height[st.top()] > height[i]) { top = st.top(); st.pop(); if (st.empty()) { area = i * height[top]; } else { area = (i - st.top() - 1) * height[top]; } if (area > max_area) { max_area = area; } } st.push(i); } } while (!st.empty()) { top = st.top(); st.pop(); if (st.empty()) { area = i * height[top]; } else { area = (i - st.top() - 1) * height[top]; } if (area > max_area) { max_area = area; } } return max_area; } };
相关文章推荐
- ADB server didn't ACK
- Maximum Subarray - LeetCode
- Stanford - Cryptography I - Week 3 Programming Assignment
- Single Number II - LeetCode
- LeetCode - Jump Game II
- LeetCode - Jump Game
- 【英语词组】attribute/ascribe/owe....to ....
- Populating Next Right Pointers in Each Node - LeetCode
- 纽约十大华人旅行社
- clang 分析OpenCL 代码
- LeetCode - Interleaving String
- Binary Tree Preorder Traversal - LeetCode
- zoj 1089 Lotto
- zoj 1089 Lotto
- Stanford - Cryptography I - Week 1-2 Programming Assignment
- linux下挂载硬盘!
- c++11 function_typetraits备忘
- hdoj1002--A + B Problem II
- unix/linux 系统实现多个进程监听同一个端口
- Linux下使用tmux