https://oj.leetcode.com/problems/container-with-most-water/

不同高度的柱子排一列,两个柱子可以组成一个容器,求最大容积。

最直观的方法就是暴力,两层for循环,分别遍历头柱子和尾柱子。但是超时了

于是看了discuss,有O(n)的方法。

class Solution {
public:
int maxArea(vector<int> &height){
if(height.size()<)
return ;
if(height.size()==)
return min(height[],height[]); int maxAns = ;
int square = ;
int low = , high = height.size()-;
while(low<high)
{
square = min(height[low],height[high])*(high-low);
if(square>maxAns)
maxAns = square;
if(height[low]<=height[high])
low++;
else
high--;
} return maxAns;
}
};

证明的话如下:对于low 和high,当 height[low]<hight[high]的时候,low往前前进了一个,这个时候就相当于对 height[low],hight[high-1]没有检查,而如果height[high-1]比hieght[high]大,则还是以height(low)为准,但是宽度上减小了一个,所以面积小了,如果height[high-1]小于height[high]则面积就更小了。所以这个漏掉是安全的。

LeetCode OJ-- Container With Most Water的更多相关文章

  1. LeetCode OJ Container With Most Water 容器的最大装水量

    题意:在坐标轴的x轴上的0,1,2,3,4....n处有n+1块木板,长度不一,任两块加上x轴即可构成一个容器,其装水面积为两板的间距与较短板长之积,以vector容器给出一系列值,分别代表在0,1, ...

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

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

  3. 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 用双指针向中间滑动,较小的高度就作为当前情 ...

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

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

  5. [LeetCode][Python]Container With Most Water

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/container-with-most-water/ Given n non-neg ...

  6. LeetCode 11. Container With Most Water (装最多水的容器)

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

  7. leetcode 【 Container With Most Water 】python 实现

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

  8. [LeetCode] 11. Container With Most Water 装最多水的容器

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

  9. 【leetcode】Container With Most Water

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

  10. LeetCode#11. Container With Most Water

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

随机推荐

  1. python3 提成计算

    题目 企业发放的奖金根据利润提成. 利润(I)低于或等于10万元时,奖金可提10%: 利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%: 20万到4 ...

  2. oracle insert用法总结

    总结下Oracle 中的Insert用法   1.标准Insert --单表单行插入   语法:     INSERT INTO table [(column1,column2,...)] VALUE ...

  3. Linux文件属性之文件权限介绍

    1)用ls -li 查看文件列表字段 红色代表的是inode 黄色代表的是文件权限 黄色里面的第一个 - 表示文件的类型(普通类型文件) d 表示目录(directory) l 表示链接文件(link ...

  4. Diycode开源项目 MyTopicActivity分析

    1.总体浏览效果及布局分析 1.1.看一下我的帖子预览效果 1.2.然后看一下我的收藏预览效果 1.3.归纳一下 所以我的帖子和我的收藏可以共用同一个类. 布局效果几乎一样,只是用户的选择不同. 所以 ...

  5. rest_framework序列化

    1.序列化 1)拿到queryset 2)将queryset 给序列化类 serializer = IdcSerializer(idc)    #单个对象 serializer = IdcSerial ...

  6. leetcode 【 Remove Element 】python 实现

    题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...

  7. 【POJ 3764】The Xor-longest Path

    题目 给定一个\(n\)个点的带权无根树,求树上异或和最大的一条路径. \(n\le 10^5\) 分析 一个简单的例子 相信大家都做过这题: 给定一个\(n\)个点的带权无根树,有\(m\)个询问, ...

  8. IOS开发学习笔记037-九宫格代码实现

    九宫格布局,用手机输入法时经常见到.先按3行3列写. 代码的实现主要是计算插入图片的位置. 每一张图片的位置和所在的行列密切相关.分析过程如下: 界面: 代码实现 1.把需要的图片资源添加进来 然后给 ...

  9. 基于单层决策树的AdaBoost算法源码

    基于单层决策树的AdaBoost算法源码 Mian.py # -*- coding: utf-8 -*- # coding: UTF-8 import numpy as np from AdaBoos ...

  10. Python之threading多线程

    1.threading模块是Python里面常用的线程模块,多线程处理任务对于提升效率非常重要,先说一下线程和进程的各种区别,如图 概括起来就是 IO密集型(不用CPU) 多线程计算密集型(用CPU) ...