算法刷题Day 60 柱状图中的最大矩阵

Day 60 单调栈

84. 柱状图中最大的矩形

暴力解法

超时了

分别找出当前位置左边第一个 比自己小的索引(的后一个位置)和右边第一个比自己小的索引(的前一个位置),这个范围之内,就是以当前位置的高度所能达到的最大宽度。

cpp 复制代码
class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        int maxArea = 0;

        for (int i = 0; i < heights.size(); ++i)
        {
            int leftLowerIdx = i, rightLowerIdx = i;

            for (int j = i; j >= 0; --j)
            {
                if (heights[j] < heights[i])
                {
                    break;
                }
                else
                {
                    leftLowerIdx = j;
                }
            }

            for (int j = i; j < heights.size(); ++j)
            {
                if (heights[j] < heights[i])
                {
                    break;
                }
                else
                {
                    rightLowerIdx = j;
                }
            }

            int area = heights[i] * (rightLowerIdx - leftLowerIdx + 1);
            if (area > maxArea)
            {
                maxArea = area;
            }
        }

        return maxArea;
    }
};

双指针

cpp 复制代码
class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        vector<int> leftLower(heights.size(), 0);
        leftLower[0] = -1; // 注意这个地方是 - 1,否则会导致死循环

        for (int i = 1; i < heights.size(); ++i)
        {
            int t = i - 1; 
            while (t >= 0 && heights[t] >= heights[i])
            {
                t = leftLower[t];
            }
            leftLower[i] = t;
        }

        vector<int> rightLower(heights.size(), 0);
        rightLower.back() = heights.size(); // 注意这个地方的值为heights.size(),否则会导致死循环

        for (int i = heights.size() - 2; i >= 0; --i)
        {
            int t = i + 1;
            while (t < heights.size() && heights[t] >= heights[i])
            {
                t = rightLower[t];
            }
            rightLower[i] = t;
        }

        int maxArea = 0;
        for (int i = 0; i < heights.size(); i++)
        {
            int area = heights[i] * (rightLower[i] - leftLower[i] - 1);
            if (area > maxArea)
            {
                maxArea = area;
            }
        }

        return maxArea;
    }
};

单调栈

有了接雨水那道题的经验,这一道题可以模仿着做出了

cpp 复制代码
class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        int maxArea = 0;
        stack<int> incStk;
        incStk.push(-1);
        heights.push_back(-1);

        for (int i = 0; i < heights.size(); ++i)
        {
            while (incStk.top() != -1 && heights[incStk.top()] > heights[i])
            {
                int idx = incStk.top();
                incStk.pop();
                int w = i - incStk.top() - 1;
                int area = heights[idx] * w;
                if (area > maxArea)
                {
                    maxArea = area;
                }
            }
            incStk.push(i);
        }

        return maxArea;
    }
};
相关推荐
foundbug9996 分钟前
基于混合整数规划的电池容量优化 - MATLAB实现
数据结构·算法·matlab
memcpy01 小时前
LeetCode 2452. 距离字典两次编辑以内的单词【暴力;字典树】中等
算法·leetcode·职场和发展
王老师青少年编程2 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【排序贪心】:魔法
c++·算法·贪心·csp·信奥赛·排序贪心·魔法
wearegogog1232 小时前
基于和差波束法的单脉冲测角MATLAB实现
人工智能·算法·matlab
AI科技星2 小时前
灵魂商数(SQ) · 全域数学统一定义【乖乖数学】
算法·机器学习·数学建模·数据挖掘·量子计算
晓觉儿2 小时前
【GPLT】2026年第十一届团队程序设计天梯赛赛后题解(已写2h,存档中)
数据结构·c++·算法·深度优先·图论
We་ct2 小时前
LeetCode 322. 零钱兑换:动态规划入门实战
前端·算法·leetcode·typescript·动态规划
6Hzlia3 小时前
【Hot 100 刷题计划】 LeetCode 394. 字符串解码 | C++ 单栈回压法
c++·算法·leetcode
穿条秋裤到处跑3 小时前
每日一道leetcode(2026.04.22):距离字典两次编辑以内的单词
算法·leetcode
淘矿人3 小时前
Claude辅助算法设计与优化
人工智能·python·算法·microsoft·github·bug·pygame