# 【LeetCode】Container With Most Water 解题报告

【题目】

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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.

【解析】

public class Solution {
public int maxArea(int[] height) {
if (height.length < 2) return 0;
int ans = 0;
int l = 0;
int r = height.length - 1;
while (l < r) {
int v = (r - l) * Math.min(height[l], height[r]);
if (v > ans) ans = v;
if (height[l] < height[r]) l++;
else r--;
}
return ans;
}
}

• 评论

12

• 上一篇
• 下一篇