給定 n 個非負整數,用來表示柱狀圖中各個柱子的高度,並且每個柱子彼此相鄰,且寬度為 1 。求在該柱狀圖中,能夠勾勒出來的矩形的最大面積。
輸入: [2,1,5,6,2,3]
輸出: 10
func largestRectangleArea(_ heights: [Int]) -> Int {
if heights.count == 0 { return 0 }
var maxArea: Int = 0
for (index, height) in heights.enumerated() {
if height <= 0 { continue }
var curMax: Int = height
// 向左延伸,找到高度大於等於當前index的最小下標
for leftIndex in (0..<index).reversed() {
if heights[leftIndex] >= height {
curMax = curMax + height
} else {
break
}
}
// 向右延伸,找到高度大於等於當前index的最大下標
if index < heights.count-1 {
for rightIndex in (index+1)..<heights.count {
if heights[rightIndex] >= height {
curMax = curMax + height
} else {
break
}
}
}
maxArea = max(curMax, maxArea)
}
return maxArea
}
func largestRectangleArea(_ heights: [Int]) -> Int {
var heights = [0] + heights + [0]
var maxArea: Int = 0
var stack: [Int] = []
for (index, height) in heights.enumerated() {
while !stack.isEmpty && stack.last != nil && heights[stack.last!] > height {
let removed = stack.removeLast()
if let leftEdge = stack.last {
var rightEdge = index
maxArea = max(maxArea, heights[removed] * (rightEdge - 1 - leftEdge))
}
}
stack.append(index)
}
return maxArea
}
typedef struct {
void **data;
int top;
int size;
} Stack;
Stack *StackCreate(int stackSize) {
Stack *stack = (Stack *)malloc(sizeof(Stack));
if (stack == NULL) {
return NULL;
}
stack->data = (void **)malloc(sizeof(void **) * (stackSize + 1));
memset(stack->data, 0, sizeof(void **) * (stackSize + 1));
stack->top = -1;
stack->size = stackSize;
return stack;
}
void StackFree(Stack *obj) {
if (obj->data != NULL) {
free(obj->data);
obj->data = NULL;
}
free(obj);
obj = NULL;
return;
}
bool IsStackEmpty(Stack *obj) {
return (obj->top == -1);
}
bool IsStackFull(Stack *obj) {
return (obj->top == obj->size);
}
// 泛型介面,使用void *
void StackPush(Stack *obj, void *data) {
if (IsStackFull(obj) == true) {
return;
}
int top = obj->top;
obj->data[++top] = data;
obj->top = top;
return;
}
void StackPop(Stack *obj) {
if (IsStackEmpty(obj) == true) {
return;
}
void *data = obj->data[obj->top];
if (data != NULL) {
free(data);
data = NULL;
}
obj->top--;
return;
}
void *StackTop(Stack *obj) {
if (IsStackEmpty(obj) == true) {
return NULL;
}
return (obj->data[obj->top]);
}
void StackClear(Stack *obj) {
if (IsStackEmpty(obj) == true) {
return;
}
for (int i = 0; i <= obj->top; i++) {
void *data = obj->data[i];
if (data != NULL) {
free(data);
data = NULL;
}
}
obj->top = -1;
return;
}
void StackPushInt(Stack *obj, int value) {
int *node = (int *)malloc(sizeof(int));
*node = value;
StackPush(obj, node);
return;
}
int StackTopInt(Stack *obj) {
if (IsStackEmpty(obj) == true) {
return -1;
}
return *(int *)(obj->data[obj->top]);
}
void StackPushStr(Stack *obj, char *str, int strSize) {
char *node = (char *)malloc(sizeof(char) * (strSize + 1));
memcpy(node, str, strSize);
node[strSize] = '\0';
StackPush(obj, node);
return;
}
char *StackTopStr(Stack *obj) {
if (IsStackEmpty(obj) == true) {
return NULL;
}
return (char *)(obj->data[obj->top]);
}
#define MAX(a, b) ((a) > (b) ? (a) : (b))
int largestRectangleArea(int* heights, int heightsSize) {
int *data = (int *)malloc(sizeof(int) * (heightsSize + 1));
memcpy(data, heights, sizeof(int) * heightsSize);
data[heightsSize] = 0;
Stack *monotoneStack = StackCreate(heightsSize + 1);
int ret = 0;
for (int i = 0; i <= heightsSize; i++) {
int top = StackTopInt(monotoneStack);
while ((IsStackEmpty(monotoneStack) != true) && (data[top] >= data[i])) {
int h = data[top];
StackPop(monotoneStack);
top = StackTopInt(monotoneStack);
int sidx = top;
ret = MAX(ret, (h * (i - sidx - 1)));
}
StackPushInt(monotoneStack, i);
}
StackFree(monotoneStack);
return ret;
}
int countHats(vector<int>& heights) {
heights.push_back(INT_MAX);
stack<int> stk;
int sum = 0;
for(int i=0; i<heights.size(); i++) {
while( !stk.empty() && heights[i] > heights[stk.top()]) ) {
int top = stk.top();
stk.pop();
sum += i – top – 1;
}
stk.push(i);
}
return sum;
}
int countSteps(vector<int>& heights) {
stack<int> stk;
vector<int> results(heights.size(), -1);
for(int i=0; i<heights.size(); i++) {
while( !stk.empty() && heights[i] > heights[stk.top()]) ) {
results[stk.top()] = i – stk.top();
stk.pop();
}
stk.push(i);
}
return results;
}
int largestRectangleArea(vector<int> heights) {
int maxArea = 0;
heights.push_back(0);
// monotone stack(ascending)
stack<int> stk;
for(int i = 0; i < heights.size(); i++) {
while(!stk.empty() && heights[i] < heights[stk.top()]) {
int top = stk.top();
// find the smaller element to the left of the current element
stk.pop();
maxArea = max(maxArea,heights[top]*(stk.empty() ? i : (i - stk.top()-1)));
}
stk.push(i);
}
return maxArea;
}