LeetCode OJ-- Container With Most Water
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的更多相关文章
- LeetCode OJ Container With Most Water 容器的最大装水量
题意:在坐标轴的x轴上的0,1,2,3,4....n处有n+1块木板,长度不一,任两块加上x轴即可构成一个容器,其装水面积为两板的间距与较短板长之积,以vector容器给出一系列值,分别代表在0,1, ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- 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 用双指针向中间滑动,较小的高度就作为当前情 ...
- Leetcode 11. Container With Most Water(逼近法)
11. Container With Most Water Medium Given n non-negative integers a1, a2, ..., an , where each repr ...
- [LeetCode][Python]Container With Most Water
# -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/container-with-most-water/ Given n non-neg ...
- LeetCode 11. Container With Most Water (装最多水的容器)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- leetcode 【 Container With Most Water 】python 实现
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- [LeetCode] 11. Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- 【leetcode】Container With Most Water
题目描述: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
- LeetCode#11. Container With Most Water
问题描述 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ...
随机推荐
- Spark架构与作业执行流程简介(scala版)
在讲spark之前,不得不详细介绍一下RDD(Resilient Distributed Dataset),打开RDD的源码,一开始的介绍如此: 字面意思就是弹性分布式数据集,是spark中最基本的数 ...
- Python知识点入门笔记——Python文件操作、异常处理及random模块使用
文件是存储在外部介质的数据集合,通常可以长久保存,前提是介质不易损坏 Python的绝对路径写法: E:\\编程学习资料\\爬取某社区高清无码大图.py E:/编程学习资料/爬取某社区高清无码大图.p ...
- 异步消息处理机制,UI更新
UI只能在主线程中完成更新,在子线程中更新UI报错如下 Only the original thread that created a view hierarchy can touch its vie ...
- Django 三—— Form组件
内容概要: 1.Django Form如何自定义验证字段 2.Django Form如何动态的显示数据库中新插入的数据 3.Tyrion Django的Form(用于验证用户请求合法性的一个组件) D ...
- 微服务化的不同阶段 Kubernetes 的不同玩法
欢迎访问网易云社区,了解更多网易技术产品运营经验. 作为容器集群管理技术竞争的大赢家,Kubernetes已经和微服务紧密联系,采用Kubernetes的企业往往都开始了微服务架构的探索.然而不同企业 ...
- 【NOIP 2017 普及组】 跳房子
裸的单调队列优化dp+二分 我居然还调了挺久 日常审题错误 #include <bits/stdc++.h> using namespace std; typedef long long ...
- uncompyle2反编译python的.py文件
前几天学用github,一不小心把a.py文件给删除了,由于1天没有提交,也无法找回.突然发现同a.py文件生成的编译文件a.pyc还在,逐去搜索一番反编译的方法. 查询得知python比较好的工具u ...
- jmeter正则表达式提取 引用
jmeter正则表达式token提取 例: 添加正则 配置 token正则表达式:"token":"(.+?)" 模板:$1$ 添加信息头管理器进行配置 需要t ...
- 并发编程——多进程——multiprocessing开启进程的方式及其属性(3)
开启进程的两种方式——Process 方式一:函数方法 from multiprocessing import Process import time def task(name): print('% ...
- Python数据分析基础——读写CSV文件2
2.2筛选特定的行: 行中的值满足某个条件 行中的值属于某个集合 行中的值匹配于某个模式(即:正则表达式) 2.2.1:行中的值满足于某个条件: 基础python版: #!/usr/bin/env p ...