Is there any Superman who can help me finding error?


#1

Comment body goes here.`public class Solution {
int area(int []heights){
Stack stack = new Stack<>();
int area= 0 ;
int garea= 0;
int i ;

    for(i = 0; i < heights.length;){
        if(stack.isEmpty() || heights[stack.peek()] <= heights[i]){
            stack.push(i++);
        }else{
            int top = stack.pop();
            if(stack.isEmpty()){
                area = heights[top] * i;
            }else{
                area = heights[top] * (i - stack.peek() -1);
            }
            
            if(area > garea){
                garea = area;
            }
        }
    }
    
    // if stack is not  empty 
    while(!stack.isEmpty()){
       int top = stack.pop();
            if(stack.isEmpty()){
                area = heights[top] * i;
            }else{
                area = heights[top] * (i - stack.peek() -1);
            }
            
            if(area > garea){
                garea = area;
            } 
    }
    
  return garea;  
    
}




public int maximalRectangle(int[][] A) {
    int row = A.length;
    int col = A[0].length;
    if(row == 0) return 0;
    if(row == 1 && col == 1)
    return A[0][0];
    
    
    int bar[] = new int[col+1];
    
    for(int i = 0  ; i < col ; i++){
        bar[i] = A[0][i];
    }
    int max = area(bar);
    int gmax= 0;
    
    for(int i = 1  ; i < row ; i++){
        for(int j = 0 ; j < col ; j++){
            if(A[i][j] == 0){
                bar[j] = 0;
            }else if(A[i][j] == 1){
                bar[j] += A[i][j] ;
            }
        }
        max = area(bar);
        if(max > gmax){gmax = max;}
        
    }
    
    // now we have histogram ...lest make the largest area
    return gmax;
    
}

}
`