zoukankan      html  css  js  c++  java
  • leetcode 84 柱状图中最大的矩形

    题目

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

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

    C++代码

    class Solution {
    public:
        int largestRectangleArea(vector<int>& heights) {
            stack<int> st; //递增栈,存下标
            int res= 0;
            int i = 0, n = heights.size();
            while(i < n)
            {
                if(st.empty() || heights[i] >= heights[st.top()])
                {
                    st.push(i);
                    i++;
                }
                else
                {
                    int k = st.top();
                    st.pop();
                    if(st.empty())
                        res = max(res, heights[k] * (i - 0));
                    else
                        res = max(res, heights[k] * (i - st.top() - 1));
                }
            }
            while(!st.empty())
            {
                int k = st.top();
                st.pop();
                if(st.empty())
                    res = max(res, heights[k] * (i - 0));
                else
                    res = max(res, heights[k] * (i - st.top() - 1));
            }
            return res;
        }
    };
  • 相关阅读:
    21分钟 MySQL 入门教程
    git学习网址
    Unsupported major.minor version 51.0解决办法
    导入Mybatis_Spring项目遇到的问题
    SQL 模糊查询
    数据持久层
    持久化框架
    ORM
    ORM框架
    重量级框架
  • 原文地址:https://www.cnblogs.com/xumaomao/p/11353020.html
Copyright © 2011-2022 走看看