《中英双解》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 the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.

Notice that you may not slant the container.

给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

说明:你不能倾斜容器。

Example 1:

Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

Example 2:

Input: height = [1,1]
Output: 1

Example 3:

Input: height = [4,3,2,1,4]
Output: 16

Example 4:

Input: height = [1,2,1]
Output: 2

 

一些单词:

coordinate :n.坐标 ,配合.v.使配合,使协调

vertical:adj.垂直的

endpoints:n.端点

x-axis:x轴

intuition n.直觉

看到这题我是蒙蔽的,压根就没有思路,看了题解才知道使用双指针,挺巧妙地解决了这个问题,确实是好聪明,我给大家简单地说一下思路。

其实就是使用两个变量来表示数组地开头和结尾地位置,然后通过Math函数,比较两者中较小地那一个位置地元素,大家都听说过木桶原理,都知道最短地那一块木板才是最终能决定乘多少的水,所以,第一次我们需要计算两数中最小的那一个数,两个木板之间的距离为1,所以下面的代码r需要自减1.然后比较出最小的那个元素,如果左边比较小,那就左边进行自增,右边较小就右边自减,再次进行比较,计算出面积,拿出这一次的面积和上一次的面积进行比较,得出最大的那个数,就是最大的容积,现在看看代码。

class Solution{
   public int maxArea(int[] height){
      int l = 0;
      int r = height.length - 1;
      int result = 0;//假设一个结果,便于比较
      while(l < r){
         int area = Math.min(height[l],height[r]) * (r - 1);//求出面积
         result = Math.max(result,area);   //找出最大的元素
         if(height[l] <= height[r]){
            l++;
         } esle {
            r--;
         }
      }
      return result;
   }
}

 这是英文的原版解答,难度不是很大,大家可以看看。

Approach 2: Two Pointer Approach

Algorithm

The intuition behind this approach is that the area formed between the lines will always be limited by the height of the shorter line. Further, the farther the lines, the more will be the area obtained.

We take two pointers, one at the beginning and one at the end of the array constituting the length of the lines. Futher, we maintain a variable \text{maxarea}maxarea to store the maximum area obtained till now. At every step, we find out the area formed between them, update \text{maxarea}maxarea and move the pointer pointing to the shorter line towards the other end by one step.

The algorithm can be better understood by looking at the example below:

1 8 6 2 5 4 8 3 7

How this approach works?

Initially we consider the area constituting the exterior most lines. Now, to maximize the area, we need to consider the area between the lines of larger lengths. If we try to move the pointer at the longer line inwards, we won't gain any increase in area, since it is limited by the shorter line. But moving the shorter line's pointer could turn out to be beneficial, as per the same argument, despite the reduction in the width. This is done since a relatively longer line obtained by moving the shorter line's pointer might overcome the reduction in area caused by the width reduction.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值