From 8e81315dfa4bee548d524448b3b0d906a59a7aca Mon Sep 17 00:00:00 2001 From: Yuki1536 <69851694+Yuki1536@users.noreply.github.com> Date: Thu, 1 Dec 2022 01:03:47 +0900 Subject: [PATCH] Add solution: problem 11. --- README.md | 2 +- src/11.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/11.c diff --git a/README.md b/README.md index 8f1ea57..63ff793 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/src/11.c b/src/11.c new file mode 100644 index 0000000..84d1fa1 --- /dev/null +++ b/src/11.c @@ -0,0 +1,32 @@ +#define _CRT_SECURE_NO_WARNINGS +#include +#include +#include +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; +} \ No newline at end of file