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

    similar with Trapping rain water

     1 public class Solution {
     2     public int largestRectangleArea(int[] height) {
     3         // IMPORTANT: Please reset any member data you declared, as
     4         // the same Solution instance will be reused for each test case.
     5         int result = 0;
     6         for(int i = 0; i < height.length; i++)
     7         {
     8             int left = i;
     9             int right = i;
    10             if(i-1 >=0 && height[i] == height[i-1])
    11                 continue;
    12             while(left > 0 && height[left-1] >= height[i])
    13                 left--;
    14             while(right < height.length-1 && height[right+1] >= height[i])
    15                 right++;
    16                 result = Math.max(result, height[i]*(right - left + 1));
    17         }
    18         return result;
    19     }
    20 }
  • 相关阅读:
    打印图形II
    打印图形
    17倍
    进制转换
    小球
    最强素数
    最强阵容
    英雄卡
    数论模板
    畅通工程 (最小生成树)(最短路径和)
  • 原文地址:https://www.cnblogs.com/jasonC/p/3412202.html
Copyright © 2011-2022 走看看