Skip to content

Commit

Permalink
Water container
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Ma <[email protected]>
  • Loading branch information
begeekmyfriend committed Nov 4, 2017
1 parent 5bb595b commit 2fde4f6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 011_container_with_most_water/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test container.c
33 changes: 33 additions & 0 deletions 011_container_with_most_water/container.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdio.h>
#include <stdlib.h>

static int maxArea(int* height, int heightSize)
{
int min = 0, max = heightSize - 1;
int area_max = 0;
while (min < max) {
int area = (max - min) * (height[min] < height[max] ? height[min] : height[max]);
area_max = area > area_max ? area : area_max;
if (height[min] < height[max]) {
while (++min < max && height[min] <= height[min - 1]) {
continue;
}
} else {
while (min < --max && height[max] <= height[max + 1]) {
continue;
}
}
}
return area_max;
}

int main(int argc, char **argv)
{
int i, count = argc - 1;
int *nums = malloc(count * sizeof(int));
for (i = 0; i < count; i++) {
nums[i] = atoi(argv[i + 1]);
}
printf("%d\n", maxArea(nums, count));
return 0;
}

0 comments on commit 2fde4f6

Please sign in to comment.