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 ...
随机推荐
- <Android 应用 之路> 天气预报(四)
前言 第二次尝试完成天气预报应用,与上次不同的是,个人感觉这次的Ui不那么丑陋,整体的实用性和界面效果,用户体验相较上一次有所提升,但是还是有很多地方需要完善. 这次使用到的内容比较丰富,包括聚合数据 ...
- SpringMVC项目的快速搭建
Spring MVC提供了一个DispatcherServlet来开发Web应用.在Servlet2.5及2以下的时候只要在web.xml下配置<servlet>元素即可. 在Servle ...
- Android list加载图片工具类
总体思路 Handler + looper + message 核心类 package com.base.imagechoose.util; import android.graphics.Bitma ...
- python基础-字符串操作
输出高亮 语法: 显示方式.前景色.背景色至少一个存在即可. 显示方式:0(关闭所有效果),1(高亮),4(下划线),5(闪烁),7(反色),8(不可见). 前景色以3开头,背景色以4开头,具体颜 ...
- php编译安装过程中遇到问题
编译安装PHP时遇到的问题 问题1: configure: error: xml2-config not found. Please check your libxml2 installation. ...
- Asp.net MVC 服务端验证多语言错误
服务端验证用户提交信息时为了实现多语言使用了资源文件,如: using System.ComponentModel.DataAnnotations; public class UserModel { ...
- python 函数学习之sys.argv[1]
一.sys 模块 sys是Python的一个「标准库」,也就是官方出的「模块」,是「System」的简写,封装了一些系统的信息和接口. 官方的文档参考:https://docs.python.org/ ...
- docker 快速搭建 mysql
准备工作 系统 centos7 切换阿里源 #备份资源文件 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo ...
- [VC]vc中socket编程步骤
sockets(套接字)编程有三种,流式套接字(SOCK_STREAM),数据报套接字(SOCK_DGRAM),原始套接字(SOCK_RAW): 基于TCP的socket编程是采用的流式套接字.在这个 ...
- xtarbackup恢复
xbstream -x < ynhw-mysql-slave.01.mysql.prod.sg_fullbak_20180326134255.xbstream -C /data/mysql cd ...