JasonChang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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 }

 

posted on 2013-11-07 12:02  JasonChang  阅读(163)  评论(0编辑  收藏  举报