【LeetCode two_pointer】11. Container With Most Water
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.
题意:给出n个非负值a1, a2, ..., an,(i, ai) 和 (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的更多相关文章
- LeetCode Array Medium 11. Container With Most Water
Description Given n non-negative integers a1, a2, ..., an , where each represents a point at coordin ...
- 【LeetCode】11. Container With Most Water 盛最多水的容器
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:盛水,容器,题解,leetcode, 力扣,python ...
- 【LeetCode】11. Container With Most Water
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- leetcode个人题解——#11 Container with most water
class Solution { public: int maxArea(vector<int>& height) { ; ; ; while(l < r) { int h ...
- 【LeetCode 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 【LeetCode练习题】Permutation Sequence
Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- 【LeetCode题解】136_只出现一次的数字
目录 [LeetCode题解]136_只出现一次的数字 描述 方法一:列表操作 思路 Java 实现 Python 实现 方法二:哈希表 思路 Java 实现 Python 实现 方法三:数学运算 思 ...
- 【LeetCode题解】7_反转整数
目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...
随机推荐
- Emacs ^ Vim
Emacs存活: http://files.cnblogs.com/files/TheRoadToTheGold/Emacs%E2%80%94%E2%80%94%E5%AD%98%E6%B4%BB.z ...
- 维护前面的position+主席树 Codeforces Round #406 (Div. 2) E
http://codeforces.com/contest/787/problem/E 题目大意:给你n块,每个块都有一个颜色,定义一个k,表示在区间[l,r]中最多有k中不同的颜色.另k=1,2,3 ...
- 搭建简单的node+express+mongodb项目
安装 首先要确保已经安装了 Node.js,接下来创建一个目录,然后进入此目录并将其作为当前工作目录. mkdir myapp cd myapp 通过 npm init 命令为应用创建一个 packa ...
- [BZOJ2809&1455&1367]解题报告|可并堆
其实非常好理解..就是可以可以合并起来的两个堆嘛>< 2809: [Apio2012]dispatching Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依 ...
- phpcms取内容发布管理中的来源
调取内容发布管理中的来源,如果直接写{$val['copyfrom']}调取出来的内容为 内容|0 ,要先根据“|”进行拆分,然后再写. 示例: <!--新闻开始--> {pc:co ...
- web上下文监听器ServletContextListener
1 package com.liveyc.common.listener; import javax.servlet.ServletContextEvent; import javax.servlet ...
- LintCode之硬币排成线
输入的n可以分为两种情况: 1. 如果n是3的倍数的话,不论A怎么拿B都可以拿(3-A拿的个数)来使其保持是3的倍数,他就一定能拿到最后一块,所以n是3的倍数的话B必胜 2. 如果n不是3的倍数的话, ...
- 【过滤器】web中过滤器的使用与乱码问题解决
一.过滤器Filter 1.filter的简介 filter是对客户端访问资源的过滤,符合条件放行,不符合条件不放行,并且可以对目 标资源访问前后进行逻辑处理 2.快速入门 步骤: 1)编写一个过 ...
- PHP对象4: final 不允许重写方法或不允许继承类
final用在方法中,能继承方法, 不允许重写方法 final用在类声名中, 此类就不能继承 <?php class A{ final function say(){ say 'Ok<br ...
- windows下制作debian U盘启动
制作平台:Windows 7 制作debian版本:debian 7.4 wheezy 1.下载引导镜像,包含三个文件:boot.img.gz(解压备用).initrd.gz 和 vmlinuz. h ...