【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.

从两边向中间考虑时,乘水的面积是由(两端较小的高度)×(两个板之间的宽度)决定的。记录最开始的乘水面积为ans1,然后L向右运动,R向左运动,截止条件是L
>= R,并且记录乘水的面积ans,取最大值

以L向左运动为例,当宽度减小时,如果面积变大,必然高度要增加,因此L只需取比前一个L大的值即可,初始L的高度为L1。R向右运动同理


public class Solution {
public int maxArea(int[] height) {
if(height.length<2)
return 0;
int re=0;
int j=height.length-1;
int i=0;
while(j>i){
if(height[i]>=height[j]){
int area = height[j]*(j-i);
if(area>re)
re=area;
j--;
continue;
}else{
int area=height[i]*(j-i);
if(area>re)
re=area;
i++;
continue;
}
}
return re;

}
}

【LeetCode】Container With Most Water,布布扣,bubuko.com

时间: 2024-08-09 02:20:26

【LeetCode】Container With Most Water的相关文章

【LeetCode】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 con

【Leetcode】Container With Most Water in JAVA

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 containe

【leetcode】Container With Most Water(middle)

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 containe

【数组】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 cont

【LeetCode】042 Trapping Rain Water

题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented

【LeetCode】数组

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum [4]Median of Two Sorted Arrays [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [26]Remove Duplicates from Sorted Array [27]Remove Element [31]Next Permutatio

【LeetCode】双指针 two_pointers(共47题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [19]Remove Nth Node From End of List [26]Remove Duplicates from Sorted

【leetcode刷提笔记】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 containe

【一天一道LeetCode】#42. Trapping Rain Water

一天一道LeetCode系列 (一)题目 Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much >water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation m