码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode – Refresh – Largest Rectangle in Histogram

时间:2015-03-20 08:02:31      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

Use two vector to record left and right indexes that can extend the blocks.

 

 1 class Solution {
 2 public:
 3     int largestRectangleArea(vector<int> &height) {
 4         int len = height.size(), result = 0;
 5         vector<int> left(len), right(len);
 6         for (int i = 0; i < len; i++) {
 7             left[i] = i;
 8             while (left[i] > 0 && height[i] <= height[left[i]-1]) left[i] = left[left[i]-1];
 9         }
10         for (int i = len-1; i >= 0; i--) {
11             right[i] = i;
12             while (right[i] < len-1 && height[i] <= height[right[i]+1]) right[i] = right[right[i]+1];
13         }
14         for (int i = 0; i < len; i++) {
15             result = max(result, height[i] * (right[i] - left[i] + 1));
16         }
17         return result;
18     }
19 };

 

LeetCode – Refresh – Largest Rectangle in Histogram

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/4352662.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!