Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution: problem 11. #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
| 14 | [Longest Common Prefix] | [C](src/14.c) |
| 13 | [Roman to Integer] | [C](src/13.c) |
| 12 | [Integer to Roman] | [C](src/12.c) |
| 11 | [Container With Most Water] | |
| 11 | [Container With Most Water] | [C](src/11.c) |
| 10 | [Regular Expression Matching] | |
| 9 | [Palindrome Number] | [C](src/9.c) |
| 8 | [String to Integer (atoi)] | [C](src/8.c) |
Expand Down
32 changes: 32 additions & 0 deletions src/11.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
int maxArea(int* height, int heightSize);

main() {
int n;
printf("Input number of height: ");
scanf("%d", &n);
int* height = (int*)malloc(sizeof(int) * n);
for (int i = 0; i < n; i++) {
printf("Input height %d: ", i);
scanf("%d", &height[i]);
}
int result = maxArea(height, n);
printf("Result: %d", result);
printf("\nOver\n");
}

int maxArea(int* height, int heightSize) {
int max = 0, area;
int i = 0, j = heightSize - 1;
while (i < j) {
if (height[i] < height[j]) area = height[i] * (j - i);
else area = height[j] * (j - i);
if (area > max) max = area;
if (height[i] < height[j]) i++;
else j--;
}
return max;
}