# 柱状图中最大的矩形

<p>给定 <em>n</em> 个非负整数，用来表示柱状图中各个柱子的高度。每个柱子彼此相邻，且宽度为 1 。</p><p>求在该柱状图中，能够勾勒出来的矩形的最大面积。</p><p>&nbsp;</p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0084.Largest%20Rectangle%20in%20Histogram/images/histogram.png"></p><p><small>以上是柱状图的示例，其中每个柱子的宽度为 1，给定的高度为&nbsp;<code>[2,1,5,6,2,3]</code>。</small></p><p>&nbsp;</p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0084.Largest%20Rectangle%20in%20Histogram/images/histogram_area.png"></p><p><small>图中阴影部分为所能勾勒出的最大矩形面积，其面积为&nbsp;<code>10</code>&nbsp;个单位。</small></p><p>&nbsp;</p><p><strong>示例:</strong></p><pre><strong>输入:</strong> [2,1,5,6,2,3]<strong><br />输出:</strong> 10</pre>

以下程序实现了这一功能，请你填补空白处的内容：

```cpp
#include <stdio.h>
#include <stdlib.h>
static int largestRectangleArea(int *heights, int heightsSize)
{
	int *indexes = malloc(heightsSize * sizeof(int));
	int *left = malloc(heightsSize * sizeof(int));
	int *right = malloc(heightsSize * sizeof(int));
	int i, pos = 0;
	for (i = 0; i < heightsSize; i++)
	{
		while (pos > 0 && heights[indexes[pos - 1]] >= heights[i])
		{
			pos--;
		}
		left[i] = pos == 0 ? -1 : indexes[pos - 1];
		indexes[pos++] = i;
	}
	pos = 0;
	for (i = heightsSize - 1; i >= 0; i--)
	{
		while (pos > 0 && heights[indexes[pos - 1]] >= heights[i])
		{
			pos--;
		}
		right[i] = pos == 0 ? heightsSize : indexes[pos - 1];
		indexes[pos++] = i;
	}
	int max_area = 0;
	_______________________;
	return max_area;
}
int main(void)
{
	int nums[] = {2, 1, 5, 6, 2, 3};
	int count = sizeof(nums) / sizeof(*nums);
	printf("%d\n", largestRectangleArea(nums, count));
	return 0;
}
```

## template

```cpp
#include <stdio.h>
#include <stdlib.h>
static int largestRectangleArea(int *heights, int heightsSize)
{
	int *indexes = malloc(heightsSize * sizeof(int));
	int *left = malloc(heightsSize * sizeof(int));
	int *right = malloc(heightsSize * sizeof(int));
	int i, pos = 0;
	for (i = 0; i < heightsSize; i++)
	{
		while (pos > 0 && heights[indexes[pos - 1]] >= heights[i])
		{
			pos--;
		}
		left[i] = pos == 0 ? -1 : indexes[pos - 1];
		indexes[pos++] = i;
	}
	pos = 0;
	for (i = heightsSize - 1; i >= 0; i--)
	{
		while (pos > 0 && heights[indexes[pos - 1]] >= heights[i])
		{
			pos--;
		}
		right[i] = pos == 0 ? heightsSize : indexes[pos - 1];
		indexes[pos++] = i;
	}
	int max_area = 0;
	for (i = 0; i < heightsSize; i++)
	{
		int area = heights[i] * (right[i] - left[i] - 1);
		max_area = area > max_area ? area : max_area;
	}
	return max_area;
}
int main(void)
{
	int nums[] = {2, 1, 5, 6, 2, 3};
	int count = sizeof(nums) / sizeof(*nums);
	printf("%d\n", largestRectangleArea(nums, count));
	return 0;
}
```

## 答案

```cpp
for (i = 0; i < heightsSize; i++)
{
	int area = heights[i] * (right[i] - left[i] - 1);
	max_area = area > max_area ? area : max_area;
}
```

## 选项

### A

```cpp
for (i = 0; i < heightsSize; i++)
{
	int area = heights[i] * (right[i] - left[i] - 1);
	max_area = area > max_area ? max_area : area;
}
```

### B

```cpp
for (i = 0; i < heightsSize; i++)
{
	int area = heights[i] * (right[i] - left[i]);
	max_area = area > max_area ? max_area : area;
}
```

### C

```cpp
for (i = 0; i < heightsSize; i++)
{
	int area = heights[i] * (right[i] - left[i]);
	max_area = area > max_area ? area : max_area;
}
```