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.

思路:两个指针指向开头和结尾的位置,每次将矮的那侧的指针往中间移动,并且只关心比原来高的情况(因为宽度已经减小了,高度必须增高才可能面积变大)

class Solution {
public:
int maxArea(vector<int>& height) {
int size = height.size();
int left = , right = height.size()-;
int maxLeft = , maxRight = ;
int ret = ; while(left < right){
if (height[left] < maxLeft) {
left++;
continue;
}
else maxLeft = height[left];
if (height[right] < maxRight) {
right--;
continue;
}
else maxRight = height[right];
ret = max(ret, min(height[left], height[right]) * (right - left)); if(height[left] < height[right]) left++;
else right--;
}
return ret;
}
};

11.Container With Most Water (Array; Two-Pointers)的更多相关文章

  1. leetcode 11. Container With Most Water 、42. Trapping Rain Water 、238. Product of Array Except Self 、407. Trapping Rain Water II

    11. Container With Most Water https://www.cnblogs.com/grandyang/p/4455109.html 用双指针向中间滑动,较小的高度就作为当前情 ...

  2. LeetCode Array Medium 11. Container With Most Water

    Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...

  3. Leetcode 11. Container With Most Water(逼近法)

    11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...

  4. 刷题11. Container With Most Water

    一.题目说明 11.Container With Most Water,这个题目难度是Medium. 二.我的做法 乍一看,简单啊,两个for循环就可以了,我在本地写的. #include<io ...

  5. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

  6. [leecode]---11.container with most water

    description: Input: [1,8,6,2,5,4,8,3,7]Output: 49 思路1: 从(1,a1)开始向后算面积,需要两层n循环,时间复杂度n2 思路2: 找出数组中最大的数 ...

  7. 《LeetBook》leetcode题解(11):Container With Most Water[M] ——用两个指针在数组内移动

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  8. Leetcode Array 11 Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  9. LeetCode 11. [👁] Container With Most Water & two pointers

    盛最多水的容器 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找 ...

随机推荐

  1. rabbitmq AmqpClient 使用Direct 交换机投递与接收消息,C++代码示例

    // 以DIRECT 交换机和ROUTING_KEY的方式进行消息的发布与订阅 // send // strUri = "amqp://guest:guest@192.168.30.11:8 ...

  2. C++ 从txt文本中读取map

    由于存入文本文件的内容都为文本格式,所以在读取内容时需要将文本格式的内容遍历到map内存中,因此在读取时需要将文本进行切分(切分成key和value) 环境gcc #include<iostre ...

  3. SpringBoot---Servlet容器(Tomcat)配置

    1.概述 1.1.Tomcat所有属性  都在org,springframework.boot.autoconfigure.web.ServerProperties配置类中作了定义: 2.替换Tomc ...

  4. 【Linux】批量结束某一脚本的进程

    ps -ef | grep **.sh |grep -v grep | awk '{print $2}' | xargs kill -9

  5. 基于Socket和OpenCV的实时视频传输

    https://blog.csdn.net/pengz0807/article/details/52204475

  6. python 对 excel 的操作

    参考:https://www.php.cn/python-tutorials-422881.html  或 https://blog.51cto.com/wangfeng7399/2339556(使用 ...

  7. Linux后台执行脚本 &与nohup

    Linux后台执行脚本的方式: 0.脚本代码 [root@VM_1_3_centos apps]# cat test.php <?php sleep(5); echo "hello w ...

  8. xml解析用正则解决没有标签的文本的解析不出异常

    如  <q>sasas<w>eqwe</w>ddas</q> package com.people.xmlToSql; import java.io.F ...

  9. Oracle 简单统计示例

    有数据如下: eg1:现在需要统计所有男性人员数量,所有女性人员数量,sclassno=10000的男性人员的总年龄,年龄大于20的女性人员数量 ----sign( number )/*If numb ...

  10. JS删除对象属性

    项目中有些属性使用之后需要重置 var user = {}; user.name = "123"; user.age="30"; console.log(use ...