Skip to content

Commit

Permalink
Largest rectangle in histogram
Browse files Browse the repository at this point in the history
Signed-off-by: begeekmyfriend <[email protected]>
  • Loading branch information
begeekmyfriend committed Sep 4, 2017
1 parent ed37657 commit d5639d4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 084_largest_rectangle_in_histogram/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test rect_in_histogram.c
31 changes: 31 additions & 0 deletions 084_largest_rectangle_in_histogram/rect_in_histogram.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdlib.h>

static int largestRectangleArea(int* heights, int heightsSize)
{
int i, max_area = 0;
for (i = 0; i < heightsSize; i++) {
if (i > 0 && heights[i - 1] == heights[i]) {
continue;
}
int low = i;
int high = i;
while (low - 1 >= 0 && heights[low - 1] >= heights[i]) {
low--;
}
while (high + 1 < heightsSize && heights[high + 1] >= heights[i]) {
high++;
}
int area = (high - low + 1) * heights[i];
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;
}

0 comments on commit d5639d4

Please sign in to comment.