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.

题意:给出n个非负值a1a2, ..., an,(iai) 和 (i, 0)组成木桶的一边,找出木桶的两边使木桶

的容积最大

思路:也是一个双指针的题

1.两个指针i,j,当i<j时,不断两头往里面扫描

2.每扫描一个位置计算当前的面积,并刷新最大的面积

3.ai和aj的小者继续往里面递进

时间复杂度O(n)

int maxArea(int* height, int heightSize) {
if(NULL==height)
return ;
int i=,j=heightSize-;
int area,t_max=;
while(i<j){
area=(j-i)*(height[i]>height[j]?height[j]:height[i]);//计算当前面积
t_max=(t_max>area?t_max:area);              //刷新最大面积
if(height[i]<height[j])                  //判断需要变化的指针
i++;
else
j--;
}
return t_max;
}

Python:

 class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
i,j = 0,len(height)-1
vol = 0
while i<j:
vol = max(vol,(j-i)*min(height[i],height[j]))
if height[i]>height[j]:
j -= 1
else:
i += 1
return vol

【LeetCode two_pointer】11. Container With Most Water的更多相关文章

  1. LeetCode Array Medium 11. Container With Most Water

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

  2. 【LeetCode】11. Container With Most Water 盛最多水的容器

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...

  3. 【LeetCode】11. Container With Most Water

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

  4. leetcode个人题解——#11 Container with most water

    class Solution { public: int maxArea(vector<int>& height) { ; ; ; while(l < r) { int h ...

  5. 【LeetCode 229】Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  6. 【LeetCode练习题】Permutation Sequence

    Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...

  7. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

  8. 【LeetCode题解】136_只出现一次的数字

    目录 [LeetCode题解]136_只出现一次的数字 描述 方法一:列表操作 思路 Java 实现 Python 实现 方法二:哈希表 思路 Java 实现 Python 实现 方法三:数学运算 思 ...

  9. 【LeetCode题解】7_反转整数

    目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...

随机推荐

  1. jQuery技术内幕 深入解析jQuery架构设计与实现原理

    jquery的外衣 jquery是一个轻量级的JS框架 //以下截取自jquery源码片段 (function( window, undefined ) { /* 源码内容 */ })( window ...

  2. jQuery插件ASP.NET应用之AjaxUpload

    本次使用AJAXUPLOAD做为上传客户端无刷上传插件,其最新版本为3.9,官方地址:http://valums.com/ajax-upload/ 在页面中引入 jquery.min.1.4.2.js ...

  3. LintCode 395: First Will Win 2

    LintCode 395: First Will Win 2 题目描述 有 n 个不同价值的硬币排成一条线.两个参赛者轮流从左边依次拿走 1 或 2 个硬币,直到没有硬币为止.计算两个人分别拿到的硬币 ...

  4. 【CODEVS】1033 蚯蚓的游戏问题

    [算法]网络流-最小费用最大流(费用流) [题解]与方格取数2类似 在S后添加辅助点S_,限流k 每条边不能重复走,限流1 #include<cstdio> #include<alg ...

  5. yii2 自动登录解读

    今日遇到一个需要将当前用户,全部登出系统(YII2框架制作)重新登录的需求 仔细回忆一遍,Yii2的登录流程,竟然有些不太明白,于是下午空闲时 重新看了下Yii2的用户登录源码 文件位于YII2项目下 ...

  6. Msql中的触发器

    解发器 当执行某种操作时解发的行为. 比如, 当表变动时触发的动作. 像商城订单, 当下单时, 库存减少. 语法: create trigger trigger_name after/befor in ...

  7. cookie 跨域的问题

    今天研究一天发现cookie无法设置除当前域名或者其父域名之外的其他domain. 这个是浏览器出于对cookie的保护造成的,也就是cookie无法跨域设置. 对于子域名也有如下规则,当前域名只能设 ...

  8. 【BubbleCup X】G:Bathroom terminal

    一个hash的题 对?出现位置直接暴力枚举,然后hash判断下,扔进map里 cf的评测机跑的针tm块 #include<bits/stdc++.h> ; ; typedef long l ...

  9. [How to] 使用HBase协处理器---基本概念和regionObserver的简单实现

    1. 简介 对于HBase的协处理器概念可由其官方博文了解:https://blogs.apache.org/hbase/entry/coprocessor_introduction 总体来说其包含两 ...

  10. python-unittest学习

    在说unittest之前,先说几个概念: TestCase 也就是测试用例 TestSuite 多个测试用例集合在一起,就是TestSuite TestLoader是用来加载TestCase到Test ...