You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This problem can be solved using **Monotonic Stack**.
@@ -167,23 +164,28 @@ var largestRectangleArea = function(heights) {
167
164
168
165
## C#
169
166
```c#
170
-
publicclassSolution {
171
-
publicintLargestRectangleArea(int[] heights) {
172
-
varmaxArea=0;
167
+
publicclassSolution
168
+
{
169
+
publicintLargestRectangleArea(int[] heights)
170
+
{
171
+
intmaxArea=0;
173
172
varindexStack=newStack<int>();
174
173
heights= [0, ..heights, 0];
175
174
176
-
for (vari=0; i<heights.Length; i++) {
177
-
while (indexStack.Count>0&&heights[i] <heights[indexStack.Peek()]) {
178
-
varpoppedIndex=indexStack.Pop();
175
+
for (vari=0; i<heights.Length; i++)
176
+
{
177
+
while (indexStack.Count>0&&heights[i] <heights[indexStack.Peek()])
178
+
{
179
+
intpoppedIndex=indexStack.Pop();
179
180
180
-
varpoppedHeight=heights[poppedIndex];
181
-
varleftIndex=indexStack.Peek(); // poppedHeight's remaining left heights are all shorter than it, because when 'poppedHeight' itself was pushed into stack, it must have caused some (could be none) taller heights been popped out of the stack.
182
-
varrightIndex=i; // poppedHeight's right heights (which are all taller than 'poppedHeight') have been popped out of the stack (disappeared) when current `i` height is being pushed into stack.
183
-
varwidth=rightIndex-leftIndex-1; // So in the range of 'width', they are all no shorter than `poppedHeight`, although they have been popped out of the stack (disappeared).
181
+
intpoppedHeight=heights[poppedIndex];
182
+
intleftIndex=indexStack.Peek(); // poppedHeight's remaining left heights are all shorter than it, because when 'poppedHeight' itself was pushed into stack, it must have caused some (could be none) taller heights been popped out of the stack.
183
+
intrightIndex=i; // poppedHeight's right heights (which are all taller than 'poppedHeight') have been popped out of the stack (disappeared) when current `i` height is being pushed into stack.
184
+
intwidth=rightIndex-leftIndex-1; // So in the range of 'width', they are all no shorter than `poppedHeight`, although they have been popped out of the stack (disappeared).
0 commit comments