代码随想录 -- day60 -- 84.柱状图中最大的矩形

84.柱状图中最大的矩形

class Solution {
public:
    int largestRectangleArea(vector& heights) {
        int sum = 0;
        for (int i = 0; i < heights.size(); i++) {
            int left = i;
            int right = i;
            for (; left >= 0; left--) {
                if (heights[left] < heights[i]) break;
            }
            for (; right < heights.size(); right++) {
                if (heights[right] < heights[i]) break;
            }
            int w = right - left - 1;
            int h = heights[i];
            sum = max(sum, w * h);
        }
        return sum;
    }
};

你可能感兴趣的:(代码随想录算法训练营,算法,leetcode,数据结构)