Need Help With this


  • 0
    R

    Just Can't understand what is the problem with this code.
    The Code runs for [2,1,5,6,6,1] but not for [2,1,5,6,3,4]

    Used the video below to understand the problem and to code on my own.
    https://www.youtube.com/watch?v=ZmnqCZp9bBs

    ANY HELP WOULD BE APPRECIATED.

    public class Solution {
        public int largestRectangleArea(int[] heights) {
            int n = heights.length;
            Stack<Integer> s = new Stack<Integer>();
            int maxArea = -1;
            int  i = 0;
            for(; i<n; i++){
                int area = 0;
                System.out.print(s);
                if (s.isEmpty() || (heights[s.peek()] <= heights[i] )){
                    s.push(i);
                }else{
                    while (!s.isEmpty()){
                        int t = s.pop();
                        if (!s.isEmpty()){
                            area = heights[t] * (i-s.peek()-1);
                        }else{
                            area = heights[t] * i;
                        }
                        maxArea = Math.max(area, maxArea);
                    }
                    s.push(i);
                }
            }
                    System.out.print(i);
            System.out.println(s);
            while(!s.isEmpty()){
                int t = s.pop();
                int area = 0;
                        if (!s.isEmpty()){
                            area = heights[t] * (i-s.peek()-1);
                        }else{
                            area = heights[t] * i;
                        }
                        maxArea = Math.max(area, maxArea);
            }
            return maxArea;
        }
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.