LeetCode Container With Most Water
2014-08-21 17:05
447 查看
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.
class Solution {
public:
int maxArea(vector<int> &height) {
int n = height.size();
if (n <= 1)
return 0;
int maxsum = 0;
int i = 0, j = n - 1;
while (i < j) {
int mini = height[i] < height[j] ? height[i] : height[j];
int temp = mini * (j - i);
if (temp > maxsum)
maxsum = temp;
if (height[i] < height[j])
i++;
else
j--;
}
return maxsum;
}
};
..., 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.
class Solution {
public:
int maxArea(vector<int> &height) {
int n = height.size();
if (n <= 1)
return 0;
int maxsum = 0;
int i = 0, j = n - 1;
while (i < j) {
int mini = height[i] < height[j] ? height[i] : height[j];
int temp = mini * (j - i);
if (temp > maxsum)
maxsum = temp;
if (height[i] < height[j])
i++;
else
j--;
}
return maxsum;
}
};
相关文章推荐
- 【LeetCode】Container With Most Water 解题报告
- leetcode—Container With Most Water
- leetcode之11. Container With Most Water(C++解法)
- leetcode:Container With Most Water(容器装更多的水)
- LeetCode 11 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
- LeetCode 11 Container With Most Water (C,C++,Java,Python)
- LeetCode 11 -- Container With Most Water
- leetcode-java.T011_ContainerWithMostWater 找两条竖线然后这两条线以及X轴构成的容器能容纳最多的水
- LeetCode Container With Most Water
- Container With Most Water —— Leetcode
- [LeetCode] Container With Most Water 简要分析
- [LeetCode-Algorithms-11] "Container With Most Water" (2017.10.2-WEEK5)
- LeetCode Container With Most Water
- leetcode 11 Container with Most Water
- LeetCode 011 Container With Most Water
- LeetCode: Container With Most Water