In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how C++ achieves the largest rectangle in the histogram". In daily operation, I believe many people have doubts about how C++ realizes the largest rectangle in the histogram. The editor consulted all kinds of data and sorted out the simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "how C++ can achieve the largest rectangle in the histogram". Next, please follow the editor to study!
The largest rectangle in the Largest Rectangle in Histogram histogram
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, 6, 2, 3].
The largest rectangle is shown in the shaded area, which has area = 10 unit.
For example
Given height = [2, 1, 5, 5, 6, 2, 3]
Return 10.
This problem makes it possible to find the largest rectangle in the histogram. At first, I thought I would use DP to solve the extremum problem, but I couldn't think of a recursive formula, so I had to give it up. If you use the violent search method to estimate that you will not be able to pass the OJ, there is a good optimization method, which is to traverse the array and find a local peak every time you find a local peak (as long as the current number is greater than the later one, then the current number is regarded as a local peak, regardless of the size of the previous number), and then traverse all the values forward to calculate the common rectangular area, keeping the maximum value for each comparison. Let's talk about why we need to deal with the local peak value. Look at the example in the topic. The local peak value is 2pc6. We only need to deal with the local peak value. Why not count at the non-local peak value? this is because in the case of the non-local peak value, the subsequent local peak value can include, for example, 1 and 5. Because the local peak 6 is higher than 1 and 5, all the rectangles that can be composed of 1 and 5. It can be formed here by 6, and you can add a part of 6 itself to form a larger rectangle, so you don't have to bother to count a rectangle that can be made up of 1 and 5. The code is as follows:
Solution 1:
/ / Pruning optimizeclass Solution {public: int largestRectangleArea (vector & height) {int res = 0; for (int I = 0; I
< height.size(); ++i) { if (i + 1 < height.size() && height[i] = 0; --j) { minH = min(minH, height[j]); int area = minH * (i - j + 1); res = max(res, area); } } return res; }}; 后来又在网上发现一种比较流行的解法,是利用栈来解,可参考其他文档,但是经过仔细研究,其核心思想跟上面那种剪枝的方法有异曲同工之妙,这里维护一个栈,用来保存递增序列,相当于上面那种方法的找局部峰值。我们可以看到,直方图矩形面积要最大的话,需要尽可能的使得连续的矩形多,并且最低一块的高度要高。有点像木桶原理一样,总是最低的那块板子决定桶的装水量。那么既然需要用单调栈来做,首先要考虑到底用递增栈,还是用递减栈来做。我们想啊,递增栈是维护递增的顺序,当遇到小于栈顶元素的数就开始处理,而递减栈正好相反,维护递减的顺序,当遇到大于栈顶元素的数开始处理。那么根据这道题的特点,我们需要按从高板子到低板子的顺序处理,先处理最高的板子,宽度为1,然后再处理旁边矮一些的板子,此时长度为2,因为之前的高板子可组成矮板子的矩形 ,因此我们需要一个递增栈,当遇到大的数字直接进栈,而当遇到小于栈顶元素的数字时,就要取出栈顶元素进行处理了,那取出的顺序就是从高板子到矮板子了,于是乎遇到的较小的数字只是一个触发,表示现在需要开始计算矩形面积了,为了使得最后一块板子也被处理,这里用了个小 trick,在高度数组最后面加上一个0,这样原先的最后一个板子也可以被处理了。由于栈顶元素是矩形的高度,那么关键就是求出来宽度,那么跟之前那道 Trapping Rain Water 一样,单调栈中不能放高度,而是需要放坐标。由于我们先取出栈中最高的板子,那么就可以先算出长度为1的矩形面积了,然后再取下一个板子,此时根据矮板子的高度算长度为2的矩形面积,以此类推,知道数字大于栈顶元素为止,再次进栈,巧妙的一比!关于单调栈问题可以参见博主的一篇总结帖 LeetCode Monotonous Stack Summary 单调栈小结,代码如下: 解法二: class Solution {public: int largestRectangleArea(vector &height) { int res = 0; stack st; height.push_back(0); for (int i = 0; i < height.size(); ++i) { if (st.empty() || height[st.top()] < height[i]) { st.push(i); } else { int cur = st.top(); st.pop(); res = max(res, height[cur] * (st.empty() ? i : (i - st.top() - 1))); --i; } } return res; }}; 我们可以将上面的方法稍作修改,使其更加简洁一些: 解法三: class Solution {public: int largestRectangleArea(vector& heights) { int res = 0; stack st; heights.push_back(0); for (int i = 0; i < heights.size(); ++i) { while (!st.empty() && heights[st.top()] >= heights [I]) {int cur = st.top (); st.pop (); res = max (res, heights [cur] * (st.empty ()? I: (I-st.top ()-1));} st.push (I);} return res;}}; at this point, the study on "how C++ achieves the largest rectangle in the histogram" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.