题解 | #直方图内最大矩形#

直方图内最大矩形

https://www.nowcoder.com/practice/bfaac4eebe5947af80912d1bcfcf2baa

#include <stack>
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param heights int整型vector 
     * @return int整型
     */
    int largestRectangleArea(vector<int>& heights) {
        int result = 0 ;
        heights.insert(heights.begin(), result) ;
        heights.push_back(result) ;
        stack<int> st ;
        st.push(0) ;

        for (int i = 1; i<heights.size(); i++) 
        {
            while (!st.empty() && heights[i] < heights[st.top()]) 
            {
                int tmp = st.top() ;
                st.pop(); 
                result = max((i-st.top()-1)*heights[tmp], result) ;
            }
            st.push(i) ;
        }
        return result ;
    }
};

左右找最小

全部评论

相关推荐

快点约我面试吧
投递百度等公司10个岗位
点赞 评论 收藏
分享
06-26 17:24
已编辑
宁波大学 golang
迷失西雅图:别给,纯kpi,别问我为什么知道
点赞 评论 收藏
分享
05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-23 14:22
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务