这是六则或许对你有些许帮助的信息:

1、阿秀在工作之余开发了一个编程资源网站,目前已经收集了很多不错的学习资源和黑科技(附带下载地址),如你有意欢迎体验以及推荐自己认为不错的资源,众人拾柴火焰高,我为人人,人人为我🔥!

2、👉23年5月份我从字节跳动离职跳槽到某外企期间,为方便自己找工作,增加上岸几率,我自己从0开发了一个互联网中大厂面试真题解析网站,包括两个前端和一个后端。能够定向查看某些公司的某些岗位面试真题,比如我想查一下行业为互联网,公司为字节跳动,考察岗位为后端,考察时间为最近一年之类的面试题有哪些?

网站地址:InterviewGuide大厂面试真题解析网站。点此可以查看该网站的视频介绍:B站视频讲解 如果可以的话求个B站三连,感谢!

3、😊 分享一个学弟发给我的20T网盘资源合集点此白嫖,主要是各类高清影视、电视剧、音乐、副业、纪录片、英语四六级考试、考研考公等资源。

4、😍免费分享阿秀个人学习计算机以来收集到的免费学习资源,点此白嫖;也记录一下自己以前买过的不错的计算机书籍、网络专栏和垃圾付费专栏;也记录一下自己以前买过的不错的计算机书籍、网络专栏和垃圾付费专栏

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

6、🔥 欢迎准备计算机校招的小伙伴加入我的学习圈子,一个人踽踽独行不如一群人报团取暖,圈子里沉淀了很多过去21/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