Container With Most Water
2014-12-31 18:01
281 查看
Given n non-negative integers a1, a2,
..., an, where each represents a point at coordinate (i, ai). n vertical
lines are drawn such that the two endpoints of line i is at (i, ai) and (i,
0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container.
..., an, where each represents a point at coordinate (i, ai). n vertical
lines are drawn such that the two endpoints of line i is at (i, ai) and (i,
0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container.
#include<stdlib.h> #include<stdio.h> int maxArea(int height[], int n) { int i = 0, j = n-1, li ,lj; int max = 0, water; while(j > i) { water = (j - i) * ((height[i] < height[j])?height[i]:height[j]); //printf("i:%d,j:%d,water:%d\n",i, j, water); if(water > max) max = water; li = height[i]; lj = height[j]; if(lj < li) while(height[j] <= lj && j > i) j--; else while(height[i] <= li && j > i) i++; } return max; } void main() { int a[] = {2, 3, 4, 1}; int n = 4; printf("%d\n", maxArea(a, n)); }
相关文章推荐
- <leetcode>Container With Most Water
- Leetcode11: Container with most water
- [leetcode] Container With Most Water
- LeetCode:Container With Most Water 算法题详解
- Leetcode 11-Container With Most Water
- LeetCode-Container With Most Water-zz
- [LeetCode]Container With Most Water(贪心)
- LeetCode--container-with-most-water
- Container With Most Water
- 11. Container With Most Water
- LeetCode(11)题解: Container With Most Water
- 11. Container With Most Water
- 11. Container With Most Water
- [Leetcode 43] 11 Container With Most Water
- 12.6—贪心法—Container With Most Water
- Leetcode-11: Container With Most Water
- 11. Container With Most Water
- LeetCode | Container With Most Water
- Leetcode 11 Container With Most Water 题解
- Container With Most Water