leetcode-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.
思路就是两点法。从最外次向里侧移动。先移动高度低的点,这样可以最大化面积
public class Solution {
public int maxArea(
int[] height) {
int ret =
0,
left =
0,
right = height.length-
1;
while(
left <
right){
ret = Math.max(Math.min(height[
left],height[
right])*(
right-
left),ret);
if(height[
left] < height[
right]){
left++;
}
else{
right--;
}
}
return ret;
}
}
转载请注明原文地址: https://ju.6miu.com/read-679086.html