zoukankan      html  css  js  c++  java
  • Largest Rectangle in Histogram

    Given n non-negative integers representing the histogram's bar height where the width of each
    bar is 1, find the area of largest rectangle in the histogram.
    For example,
    Given height = [2,1,5,6,2,3],
    return 10.

    Solution: 1. Only calucate area when reaching local maximum value.
    2. Keep a non-descending stack. O(n).

     1 class Solution {
     2 public:
     3     int largestRectangleArea(vector<int> &height) {
     4         int N = height.size(); 
     5         int res = 0;
     6         for(int i = 0; i < N; i++) {
     7             if(i < N - 1 && height[i] <= height[i+1])
     8                 continue;
     9             int minHeight = INT_MAX;
    10             for(int j = i; j >= 0; j--) {
    11                 minHeight = min(minHeight, height[j]);
    12                 res = max((i-j+1)*minHeight, res);
    13             }
    14         }
    15         return res;
    16     }
    17 };
  • 相关阅读:
    C语言本身并不提供输入输出语句
    大数据
    kdd cup 论文
    决策树比较
    推荐系统
    geohash
    MySQLdb 安装
    天池大数据比赛
    逻辑回归
    矩阵分解
  • 原文地址:https://www.cnblogs.com/zhengjiankang/p/3653740.html
Copyright © 2011-2022 走看看