• + 0 comments

    //C# Solution

    public static long largestRectangle(List<int> h)
        {
            int highScore = 0;
            List<Tuple<int, int>> items = new List<Tuple<int, int>>();
            
            for(int i = 0; i < h.Count; i++)
            {
                int start = i;
                while(items.Count > 0 && items[items.Count - 1].Item2 > h[i])
                {
                    Tuple<int, int> topOfStack = items[items.Count - 1];
                    items.RemoveAt(items.Count - 1);
                    highScore = Math.Max(highScore, topOfStack.Item2 * (i - topOfStack.Item1));
                    start = topOfStack.Item1;
                }
                items.Add(new Tuple<int, int>(start, h[i]));
            }
            
            foreach(Tuple<int, int> item in items)
            {
                highScore = Math.Max(highScore, item.Item2 * (h.Count - item.Item1));
            }
            
            return highScore;
        }