4/13/2014

Leetcode -- Largest Rectangle in Histogram

Brutal force, timeout
class Solution {  
 public:  
   int largestRectangleArea(vector<int> &height) {  
     int n = height.size();  
     int max = 0;  
     for(int i = 0; i < n; ++i) {  
       int h = height[i];  
       for(int j = i; j < n; j ++) {  
         if(height[j] <= height[i]) {  
           h = height[j];  
         }    
         int newArea = h * (j - i + 1);  
         if(newArea > max) {  
           max = newArea;  
         }  
       }  
     }  
     return max;  
   }  
 };  

No comments: