每天一题LeetCode[第八天]

每天一题LeetCode[第八天]


Container with Most water

Description:
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.

Note: You may not slant the container and n is at least 2.

Subscribe to see which companies asked this question.
中文翻译:
 给n个非负数的整数,代表坐标轴的点(i,ai)。想象有n个垂直直线被绘画在坐标上,就像两个端点直线 在点(i,ai),(i,0)。找到两条直线,使得它们与x轴坐标组成一个容器,这个容器包含的水是最多的。
 提醒:你不能倾斜这个容器并且n的最小值为2

解题思路:
  • 首选题目一开始又看错了。。。以后文章都要手动翻译成中文的,以提高英文阅读能力。

  • 在明白题意后,题意就是找最大面积矩形,大致思路如下:从两边向里面逼近,然后每一步如何前进呢?这要想清楚,两边那一边动呢? 想想下,如果两端对应的高度不一样,为了能找到最大边,是不是应该把最小一高度一端的距离 变化,才能保证每一步找的都是最大矩形的最大可能的下一个矩形。按着这个思路,撸码,借鉴top solution:


Java代码:
public int maxArea(int [] height){
        int maxArea=0;
        int left=0,right=height.length-1;
        while (left<right){
            maxArea=Math.max(maxArea,Math.min(height[left],height[right])*(right-left));
            if(height[left]>height[right]){
                right--;
            }else{
                left++;
            }
        }
        return maxArea;
    }

提高代码质量就是:积累精美的思路,优质的代码的过程。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值