这是四则或许对你有帮助的讯息

1、👉 最近我发现了一个每日都会推送最新校招资讯的《校招日程》文档,其中包括往届补录应届实习校招信息,比如各种大厂、国企、银行、事业编等信息都会定期更新,帮忙扩散一下。

2、😍 免费分享阿秀个人学习计算机以来的收集到的免费资源,点此白嫖

3、🚀如果你想在校招中顺利拿到更好的offer,阿秀建议你多看看前人踩过的坑留下的经验,事实上你现在遇到的大多数问题你的学长学姐师兄师姐基本都已经遇到过了。

4、🔥 欢迎准备计算机校招的小伙伴加入我的学习圈子,一个人踽踽独行真的不如一群人报团取暖,过去22届和23届的小伙伴好好跟着走下去的,最后基本都拿到了不错的offer!如果你需要《阿秀的学习笔记》网站中📚︎校招八股文相关知识点的PDF版本的话,可以点此下载

# 84. 柱状图中最大的矩形 单调栈,很经典的题目

力扣原题链接(点我直达) (opens new window)

给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。

求在该柱状图中,能够勾勒出来的矩形的最大面积。

以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为 [2,1,5,6,2,3]

图中阴影部分为所能勾勒出的最大矩形面积,其面积为 10 个单位。

示例:

输入: [2,1,5,6,2,3]
输出: 10
1
2

# 第一版,看的解答,单调栈

解析:

https://blog.csdn.net/Zolewit/article/details/88863970

执行用时 :16 ms, 在所有 cpp 提交中击败了78.93%的用户

内存消耗 :10.4 MB, 在所有 cpp 提交中击败了52.48%的用户

int largestRectangleArea(vector<int>& heights) {
	stack<int> st;
	heights.push_back(0);
	int res = 0,temp;
	for (int i = 0; i < heights.size(); ++i) {
		while (!st.empty() && heights[st.top()] >= heights[i]) {
			cout << st.top() << " 出栈,大小为" << heights[st.top()] << endl;;
			temp = st.top();
			st.pop();
			res = max(res, heights[temp] * (st.empty() ? i : (i - st.top() - 1)));
			cout << "maxS:" << res << endl;
		}
		st.push(i);
		cout << i << " 进栈,大小为" << heights[i] << endl;
	}
	return res;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

0 进栈,大小为2 0 出栈,大小为2 maxS:2 1 进栈,大小为1 2 进栈,大小为5 3 进栈,大小为6 3 出栈,大小为6 maxS:6 2 出栈,大小为5 maxS:10 4 进栈,大小为2 5 进栈,大小为3 5 出栈,大小为3 maxS:10 4 出栈,大小为2 maxS:10 1 出栈,大小为1 maxS:10 6 进栈,大小为0 10请按任意键继续. . .

# 二刷:1、暴力法超时了

    int largestRectangleArea(vector<int>& heights) {
        int len=heights.size();
        if(len==0) return 0;
        if(len==1) return heights[0];
        int maxArea = -1;
        for(int i=0;i<len;++i){

            int curHeight = heights[i];
            for(int j=i;j<len;++j){

                if(curHeight>heights[j]) curHeight = heights[j];
                int area = curHeight*(j-i+1);
                if(maxArea<area) maxArea = area;

            }

        }
        return maxArea;
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 二刷2、这种做法真的超级好,要善于利用以前的结果

# 2.1原生版暴力法超时

 int largestRectangleArea(vector<int>& heights) {
	int len = heights.size();
	if (len == 0) return 0;
	if (len == 1) return heights[0];
	int maxArea = -1;
	vector<int> left(len, 0), right(len, 0);//每个节点左右两边能到达不小于自己高度的最大距离
	for (int i = 0; i < len; ++i) {

		int bound = i;
		while (bound - 1 >= 0 && heights[bound - 1] >= heights[i]) bound--;
		left[i] = bound;

		bound = i;
		while (bound + 1 < len && heights[bound + 1] >= heights[i]) bound++;
		right[i] = bound;
	}

	for (int i = 0; i < len; ++i) {
		maxArea = max(maxArea, (right[i] - left[i] + 1) * heights[i]);
		//cout << (right[i] - left[i] + 1) * heights[i]<<" "<<maxArea << endl;
	}
	return maxArea;
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 2.2改良版

执行用时:4 ms, 在所有 C++ 提交中击败了99.99%的用户

内存消耗:8.1 MB, 在所有 C++ 提交中击败了100.00%的用户

int largestRectangleArea(vector<int>& heights) {
	int len = heights.size();
	if (len == 0) return 0;
	if (len == 1) return heights[0];
	int maxArea = -1;
	vector<int> left(len, 0), right(len, 0);//每个节点左右两边能到达不小于自己高度的最大距离
	for (int i = 0; i < len; ++i) {

		int bound = i;
		while (bound > 0 && heights[bound - 1] >= heights[i]) bound=left[bound-1];//,如果说bound -1 的值已经很小了,直接用就行,就不用再自己慢慢遍历了,左边最小就是0了,右边最大也就是len-1
		//要善于利用已经得到的结果
		left[i] = bound;
	}

	for (int i = len-1; i >=0 ; --i) {
		int bound = i;
		while (bound < len - 1 && heights[bound + 1] >= heights[i]) bound = right[bound + 1];
		right[i] = bound;
	}
	for (int i = 0; i < len; ++i) {
		maxArea = max(maxArea, (right[i] - left[i] + 1) * heights[i]);
	}
	return maxArea;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# 三刷:

    int largestRectangleArea(vector<int>& heights) {
        int len = heights.size();
        if(len == 0) return 0;
        if(len == 1) return heights[0];
        int maxArea = -1, bound = 0;
        vector<int> left(len,0), right(len,0);
        for(int i = 0; i <= len-1; ++i){
            bound = i;
            while(bound >=1 && heights[bound - 1] >= heights[i]) bound = left[bound - 1];
            left[i] = bound;

        }

        for(int i = len - 1; i >= 0; --i){
            bound = i;
            while(bound <len - 1 && heights[bound + 1] >=heights[i]) bound = right[bound + 1];
            right[i] = bound;

        }

        for(int i = 0; i < len; ++i){
            maxArea = max(maxArea,(right[i] - left[i] +1 ) * heights[i]);
        }

        return maxArea;
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26