当前位置: 首页 > article >正文

柱状图中最大的矩形 - 困难

*************

c++

topic: 84. 柱状图中最大的矩形 - 力扣(LeetCode)

*************

chenck the topic first:

Think about the topics I have done before. the rains project comes:盛最多水的容器 - 中等难度-CSDN博客icon-default.png?t=O83Ahttps://blog.csdn.net/ElseWhereR/article/details/144420793?spm=1001.2014.3001.5501

Double pointer was used. Here in this project double can be used to.

The most important is to caculate the area. So named variable area is area. Initialize the area 0.

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        
        int area = 0;

        // do something here
    }
};

 area = length × heigth;

height is eazy to be seen which is min(heights[i], height[j]);

length is easy too which is j - i + 1;

next step is how to loop? 

refer to the rains problem:

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        int n = heights.size();
        int max_area = 0;

        for (int i = 0; i < n; i++) {
            int minHeight = heights[i];  // 初始化最小高度为当前柱子的高度
            for (int j = i; j >= 0; j--) {  // 内层循环应从i向左遍历
                if (heights[j] < minHeight) {
                    minHeight = heights[j];
                }
                int width = i - j + 1;
                int area = minHeight * width;
                if (area > max_area) {
                    max_area = area;
                }
            }
        }

        return max_area;
    }
};

and overtime comes like old friend:

 aesthetics of violence always works.

Change the way to reduce the loop times.

Introduce Mr. Stack:

not that stack, but this  stack:

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        stack<int> s;  // 存储柱子的下标
        int max_area = 0;
        int n = heights.size();
        
        for (int i = 0; i <= n; i++) {
            int h = (i < n) ? heights[i] : 0;
            while (!s.empty() && h < heights[s.top()]) {
                int top = s.top();
                s.pop();
                int left = s.empty() ? -1 : s.top();
                int width = i - left - 1;
                int area = heights[top] * width;
                max_area = max(max_area, area);
            }
            s.push(i);
        }
        
        return max_area;
    }
};

In c++, stack means one type of data structure. The stack structure is used to 存储函数调用时的参数、局部变量和返回地址。每当一个函数被调用时,相关的信息被压入栈中;当函数执行完毕后,这些信息被弹出栈,以便返回到调用函数。

Stack kickss pointer j out.

  1. The important thing is making a stack, the stack is progressive increase.
  2. When a height smaller than the top of the stack is encountered, the top of the stack is popped and the area of the rectangle with that height as the lowest is calculated.
  3. refresh the max_area.

For example: 

heights = [2, 1, 5, 6, 2, 3]

i012345
h215623
    • i = 0:

      • h = heights[0] = 2

      • 栈为空,直接压入0,栈 = [0]。

    • i = 1:

      • h = heights[1] = 1

      • 1 < heights[0] = 2,弹出0,计算面积:

        • left = -1width = 1 - (-1) - 1 = 1area = 2 * 1 = 2

      • 压入1,栈 = [1]。

    • i = 2:

      • h = heights[2] = 5

      • 5 >= heights[1] = 1,直接压入2,栈 = [1, 2]。

    • i = 3:

      • h = heights[3] = 6

      • 6 >= heights[2] = 5,直接压入3,栈 = [1, 2, 3]。

    • i = 4:

      • h = heights[4] = 2

      • 2 < heights[3] = 6,弹出3,计算面积:

        • left = 2width = 4 - 2 - 1 = 1area = 6 * 1 = 6max_area = 6

      • 2 < heights[2] = 5,弹出2,计算面积:

        • left = 1width = 4 - 1 - 1 = 2area = 5 * 2 = 10max_area = 10

      • 压入4,栈 = [1, 4]。

    • i = 5:

      • h = heights[5] = 3

      • 3 >= heights[4] = 2,直接压入5,栈 = [1, 4, 5]。

    • i = 6:

      • h = 0

      • 0 < heights[5] = 3,弹出5,计算面积:

        • left = 4width = 6 - 4 - 1 = 1area = 3 * 1 = 3max_area = 10

      • 0 < heights[4] = 2,弹出4,计算面积:

        • left = 1width = 6 - 1 - 1 = 4area = 2 * 4 = 8max_area = 10

      • 0 < heights[1] = 1,弹出1,计算面积:

        • left = -1width = 6 - (-1) - 1 = 6area = 1 * 6 = 6max_area = 10

      • 压入6,栈 = [6]。

  1. 返回最大面积

    • max_area = 10

It works. And the next step is to write the code:

the first is to innitialize some variable

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        stack<int> s;  // 存储柱子的下标
        int max_area = 0;
        int n = heights.size();
        
       // do something here

    }
};

and caculate the area :

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        stack<int> s;  // 存储柱子的下标
        int max_area = 0;
        int n = heights.size();
        
        // do something here
       
                int area = heights[top] * width; // caculate the area
                max_area = max(max_area, area);  // fresh the max area
           
    }
};

how to find the width?

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        stack<int> s;  // 存储柱子的下标
        int max_area = 0;
        int n = heights.size();
        
        // do something here
       
                int width = i - left - 1; // find the width
                int area = heights[top] * width; // caculate the area
                max_area = max(max_area, area);  // fresh the max area
           
    }
};

and what is left?

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        stack<int> s;  // 存储柱子的下标
        int max_area = 0;
        int n = heights.size();
        
        // do something here
       
                int left;
                if (s.empty()) {
                    left = -1;
                } else {
                    left = s.top();
                }
                int width = i - left - 1; // find the width
                int area = heights[top] * width; // caculate the area
                max_area = max(max_area, area);  // fresh the max area
       
    }
};

make the loop

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        stack<int> s;  // 存储柱子的下标
        int max_area = 0;
        int n = heights.size();
        
        for (int i = 0; i <= n; i++) {
            int h;
            if (i < n) {
                h = heights[i];
            } else {
                h = 0;
            }
            
           
                        
                        int left;
                        if (s.empty()) {
                            left = -1;
                        } else {
                            left = s.top();
                        }
                       
                        int width = i - left - 1; // find the width
                        int area = heights[top] * width; // calculate the area
                        max_area = max(max_area, area);  // refresh the max area
                    } else {
                        break; // 如果h不小于栈顶元素的高度,退出循环
                    }
                } 
        
        return max_area;
    }
};

int h

class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        stack<int> s;  // 存储柱子的下标
        int max_area = 0;
        int n = heights.size();
        
        for (int i = 0; i <= n; i++) {
            int h;
            if (i < n) {
                h = heights[i];
            } else {
                h = 0;
            }
            
            
            while (true) {
                if (!s.empty()) {
                    if (h < heights[s.top()]) {
                        int top = s.top();
                        s.pop(); // kick your ass
                        
                        int left;
                        if (s.empty()) {
                            left = -1;
                        } else {
                            left = s.top();
                        }
                       
                        int width = i - left - 1; // find the width
                        int area = heights[top] * width; // calculate the area
                        max_area = max(max_area, area);  // refresh the max area
                    } else {
                        break; // 如果h不小于栈顶元素的高度,退出循环
                    }
                } else {
                    break; // 如果栈为空,退出循环
                }
            }
            s.push(i); // add it
        }
        
        return max_area;
    }
};

done


http://www.kler.cn/a/460616.html

相关文章:

  • Spark Runtime Filter
  • vue v-for 数据增加页面不刷新
  • 创建线程的8种方法
  • 面试场景题系列:设计视频分享系统
  • Python世界:人生苦短,我用Python
  • 为什么需要设置 `NCCL_P2P_DISABLE=1` 和 `NCCL_IB_DISABLE=1`?
  • 微服务-Sentinel新手入门指南
  • UE5在蓝图中使用VarestX插件访问API
  • html+css网页制作 美食 美食每刻4个页面
  • MapReduce相关概念(自用)
  • 抖音电商全年销售154亿单产业带商品,830个产业带销售额过亿
  • 【每日学点鸿蒙知识】箭头函数、Watch状态变量、H5获取定位数据、前后台切换、长按事件
  • HarmonyOS Next 应用元服务开发-应用接续动态配置迁移快速启动目标应用
  • 【linux学习指南】Ext系列文件系统(二)引⼊⽂件系统“块“分区inode概念
  • 老年认知衰弱分类模型在临床即时检测系统中的应用
  • R语言文件IO和并行计算优化实践
  • 在【IntelliJ IDEA】中配置【Tomcat】【2023版】【中文】【图文详解】
  • 大语言模型(LLM)一般训练过程
  • 压测--使用jmeter、nmon、nmon analysis进行压测与分析
  • 开源AI智能名片2+1链动模式O2O商城小程序:以情感共鸣驱动用户归属与品牌建设的深度探索
  • 视频首页uniapp
  • MySQL三层B+树能存多少数据
  • HttpServlet类的继承与doGet、doPost等方法的重写
  • Docker搭建Skywalking
  • 基于云计算的大数据项目实训室创新建设方案
  • 2025决战智驾:从中阶卷到L3,车企需要抓好一个数据闭环