Container With Most Water
来源:互联网 发布:js 公式编辑器 编辑:程序博客网 时间:2024/06/05 02:24
11. Container With Most Water
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.
strategy
1. the area of container should be the height = min(ai, aj) times the distance between two point at x-axis
2. using for loop to search for the two point in the range between 1 and n
public class Solution {
public int maxArea(int[] height) {
if (height == null || height.length == 0)
return 0;
int len = height.length;
int low = 0, high = len - 1, maxArea = -1;
while (low < high) {
int currentArea = (high - low) * (Integer.min(height[low], height[high]));
maxArea = Integer.max(maxArea, currentArea);
if (height[low] <= height[high])
low++;
else
high--;
}
return maxArea;
}
}
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.
strategy
1. the area of container should be the height = min(ai, aj) times the distance between two point at x-axis
2. using for loop to search for the two point in the range between 1 and n
public class Solution {
public int maxArea(int[] height) {
if (height == null || height.length == 0)
return 0;
int len = height.length;
int low = 0, high = len - 1, maxArea = -1;
while (low < high) {
int currentArea = (high - low) * (Integer.min(height[low], height[high]));
maxArea = Integer.max(maxArea, currentArea);
if (height[low] <= height[high])
low++;
else
high--;
}
return maxArea;
}
}
0 0
- Container With Most Water
- Container with most water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- Container With Most Water
- 设计模式(13)-桥接模式
- 图的存储以及深度优先和广度优先
- C++异常中生命周期回顾
- 我的archlinux软件列表
- C语言+鞍点
- Container With Most Water
- 适配器模式
- C# 颜色表
- 【Java】java集合框架
- 黑马程序员——java基础之多态
- 图像处理与计算机视觉:基础,经典以及最近发展(5)计算机视觉
- leetcode之Spiral Matrix II
- 初识JavaScript,Ajax,jQuery,并比较三者关系
- 读OpenCV之小侃高斯滤波