leetcode 【 Container With Most Water 】python 实现
题目:
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.
代码:oj测试通过 Runtime: 132 ms
class Solution:
# @return an integer
def maxArea(self, height):
# none case or one element case
if height is None or len(height)<2:
return 0
# left point and right point
left = 0
right = len(height)-1
max_container = 0
while left<right :
curr_container = min(height[left],height[right]) * (right-left)
max_container = max(curr_container,max_container)
if height[left]>height[right]:
right = right-1
else:
left = left+1
return max_container
思路:
数组前后双指针技巧。
有点儿像动态规划。
两个指针一左一右left right
面积为:min(height[left],height[right])*(right-left)
指针迭代条件为:哪边的指针所指位置的高度小,就从哪边往中间移动。每一步更新一次max_container的值。
为什么哪边的指针所指的位置高度小就从哪边往中间移动呢?能装多少水是有较短的那边决定的,因此如果寻求装更多的水,则应该优先从较短的一侧开始求变。
这样一来,每一次迭代后,都保证max_container保存了当前以及之前的可能最大蓄水量。
leetcode 【 Container With Most Water 】python 实现的更多相关文章
- leetcode Container With Most Water python
class Solution(object): def maxArea(self, height): """ :type height: List[int] :rtype ...
- LeetCode:Container With Most Water,Trapping Rain Water
Container With Most Water 题目链接 Given n non-negative integers a1, a2, ..., an, where each represents ...
- [LeetCode] 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 简要分析
前言 这题非要说贪心的话也算是吧,不过最主要的特征还是双指针.LC的题好像不少都是扔倆头尾指针然后遍历一遍完事儿的.这道题倒是“短板效应”的不错体现了. 题目 题目链接 Given n non-neg ...
- [LeetCode]Container With Most Water, 解题报告
前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n no ...
- C++LeetCode:: Container With Most Water
本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做.感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过.每天都要被LeetCo ...
- leetcode 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 ( C++)
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- LeetCode——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 (Two Pointers)
题意 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
随机推荐
- C#添加删除防火墙例外(程序、端口)
一. 添加 COM 引用 在引用里,选择 COM 页, 找到 NetFwTypeLib , 确定即可 二. 添加允许通过防火墙的例外程序 using System; using System.Coll ...
- 2019年5~6月训练记录(更新ing)
前言 \(ZJOI\)正式结束了. 但期中考试只考了年级\(216\),退役既视感... 于是就被抓回去补文化课了. 下半个学期可能要以文化课为主了吧! 但周三.周日应该还是会正常参加训练的,但其他时 ...
- 简析平衡树(一)——替罪羊树 Scapegoat Tree
前言 平衡树在我的心目中,一直都是一个很高深莫测的数据结构.不过,由于最近做的题目的题解中经常出现"平衡树"这三个字,我决定从最简单的替罪羊树开始,好好学习平衡树. 简介 替罪羊树 ...
- IPC Gateway 设计
1. IPC Gateway对外提供的功能: IPC的register/request/reply/notification服务. 2. IPC Gatew的实现原理: 各个具体的服务注册自己的回调函 ...
- 五、react中父子组件间如何传值
1.父组件向子组件传递数据:父组件绑定属性值传给子组件,子组件通过this.props()接受. 2.子组件向父组件传递数据:子组件绑定一个方法,方法中通过this.props.父组件方法名(参数)传 ...
- 2017.12.14 Java实现-----图书管理系统
通过对图书的增删改查操作 用数组实现 Manager类 package demo55; import java.util.*; public class Manager { Scanner sc = ...
- python 线程even
import threading,time import random def door(nums): num=1#电梯在一楼 while True: print("this door is ...
- H1ctf-Vote
用来练习IO_FILE利用 glibc-2.23 # coding:utf-8 from pwn import * from FILE import * context.arch = 'amd64' ...
- PAT (Advanced Level) Practise - 1095. Cars on Campus (30)
http://www.patest.cn/contests/pat-a-practise/1095 Zhejiang University has 6 campuses and a lot of ga ...
- JDBC开发,数据库的连接
JDBC开发步骤 1.建完工程后,右击工程,new,新建一个文件夹Folder,装jar包, 2.将下载好的驱动包打开,找到jar文件,CTRL+C,选中装jar包的文件夹,CTRL+V ...