-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution84.cs
32 lines (26 loc) · 893 Bytes
/
Solution84.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution84
{
/// <summary>
/// 84. Largest Rectangle in Histogram - Medium
/// <a href="https://leetcode.com/problems/largest-rectangle-in-histogram">See the problem</a>
/// </summary>
public int LargestRectangleArea(int[] heights)
{
var stack = new Stack<int>();
var maxArea = 0;
for (var i = 0; i <= heights.Length; i++)
{
var currentHeight = i == heights.Length ? 0 : heights[i];
while (stack.Count > 0 && heights[stack.Peek()] > currentHeight)
{
var height = heights[stack.Pop()];
var width = stack.Count == 0 ? i : i - stack.Peek() - 1;
maxArea = Math.Max(maxArea, height * width);
}
stack.Push(i);
}
return maxArea;
}
}