forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0084-largest-rectangle-in-histogram.c
77 lines (69 loc) · 1.33 KB
/
0084-largest-rectangle-in-histogram.c
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const int MAX_N = 1e5 + 5;
struct Stack
{
int top;
int capacity;
int *array;
};
struct Stack *createStack()
{
struct Stack *stack = (struct Stack *)malloc(sizeof(struct Stack));
stack->capacity = MAX_N;
stack->top = -1;
stack->array = (int *)malloc(stack->capacity * sizeof(int));
return stack;
}
int isFull(struct Stack *stack)
{
return stack->top == stack->capacity - 1;
}
int isEmpty(struct Stack *stack)
{
return stack->top == -1;
}
void push(struct Stack *stack, int val)
{
if (isFull(stack))
return;
stack->array[++stack->top] = val;
}
void pop(struct Stack *stack)
{
if (isEmpty(stack))
return;
--stack->top;
}
int peek(struct Stack *stack)
{
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top];
}
int max(int num1, int num2)
{
return (num1 > num2) ? num1 : num2;
}
int largestRectangleArea(int *heights, int heightsSize)
{
int ans = 0;
struct Stack *st = createStack();
for (int i = 0; i < heightsSize; i++)
{
while (!isEmpty(st) && heights[peek(st)] > heights[i])
{
int tp = peek(st);
pop(st);
int dist = (isEmpty(st) ? i : i - peek(st) - 1);
ans = max(ans, dist * heights[tp]);
}
push(st, i);
}
while (!isEmpty(st))
{
int tp = peek(st);
pop(st);
int dist = (isEmpty(st) ? heightsSize : heightsSize - peek(st) - 1);
ans = max(ans, dist * heights[tp]);
}
return ans;
}