Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Histogram
- class Solution {
- public:
- typedef vector<int> vec_t;
- int largestRectangleArea(vec_t & histogram) {
- if(histogram.empty()) { return 0; }
- const size_t N = histogram.size();
- vec_t maxSoFarLeft(N);
- vec_t maxSoFarRight(N);
- maxSoFarLeft[0] = histogram[0];
- maxSoFarLeft[N-1] = histogram[N-1];
- for(size_t i = 1; i < N; ++i)
- {
- int H = histogram[i];
- size_t j = N-1-i;
- maxSoFarLeft[i] = max(histogram[i],maxSoFarLeft[i-1]);
- maxSoFarLeft[i] = max(histogram[j],maxSoFarRight[j+1]);
- }
- int maxArea = INT_MIN;
- for(size_t pos = 0; pos < N; ++pos)
- {
- const int H = histogram[pos];
- typedef vec_t::iterator iter_t;
- iter_t begin_range = std::lower_bound(maxSoFarLeft.begin(), maxSoFarLeft.begin() + pos, H);
- int beg_pos = std::distance(maxSoFarLeft.begin(), begin_range);
- iter_t end_range = std::upper_bound(maxSoFarRight.begin()+pos+1, maxSoFarRight.end(), H);
- int end_pos = std::distance(maxSoFarRight.begin(), end_range);
- int area = H * (end_pos - beg_pos);
- maxArea = std::max(maxArea, area);
- }
- return maxArea;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment