Leetcode11 Container With Most Water 解题思路 (Python)
今天开始第一天记录刷题,本人编程小白,如果有写的不对、或者能更完善的地方请个位批评指正!
准备按tag刷,第一个tag是array:
这个是array的第一道题: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 and n is at least 2.
Example:
Input: [1,8,6,2,5,4,8,3,7]
Output: 49
思路:
这道题是找:(取两个数组元素中比较小的哪个数字)*两数组元素之间宽度(i.e.,|最大数字的数组下标-最小数字的数字下标+1|)的最大值
除了可以用嵌套for循环之外(时间复杂度O(n^2)),我的想法是:
第一步:先用数组中的第一个元素和最后一个元素来计算,因为这个时候的宽度是最宽的
第二至第n步:然后取第一个和最后一个数字中相对大一点的数字,依次向中间渐进。
结束条件:第一个数向后渐进的数组下标等于最后一个数向前渐进的数组的下标。
这样的话可以实现复杂度:O(n)
Python 代码实现:
class Solution(object):
def maxArea(self, height):
"""
:type height: List[int]
:rtype: int
"""
front_index = 0
end_index = len(height) - 1
water = (len(height)-2)*min(height[len(height)-1],height[0])
while(front_index != end_index):
water_temp = (end_index - front_index)*min(height[front_index],height[end_index])
if height[front_index] > height[end_index]:
end_index -= 1
if water_temp >= water:
water = water_temp
elif height[front_index] <= height[end_index]:
front_index += 1
if water_temp >= water:
water = water_temp
return water
height = [1,8,6,2,5,4,8,3,7]
print(maxArea(0,height))
Leetcode11 Container With Most Water 解题思路 (Python)的更多相关文章
- LeetCode11 Container With Most Water
题意: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- [LeetCode]Container With Most Water, 解题报告
前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n no ...
- 思维题(两点逼近)LeetCode11 Container with Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode] 42. Trapping Rain Water 解题思路
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...
- LeetCode Two Sum 解题思路(python)
问题描述 给定一个整数数组, 返回两个数字的索引, 使两个数字相加为到特定值. 您可以假设每个输入都有一个解决方案, 并且您不能使用相同的元素两次. 方法 1: 蛮力 蛮力方法很简单.循环遍历每个元素 ...
- Leetcode11.Container With Most Water盛最多水的容器
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...
- leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number
1. Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...
随机推荐
- 【spring】使用spring过程中踩到的坑
这里简单记录一下,学习spring的时候碰过的异常: 异常:org.springframework.beans.factory.BeanDefinitionStoreException: Unexpe ...
- innodb_flush_log_at_trx_commit与sync_binlog理解
innodb_flush_log_at_trx_commit该参数控制重做日志写入磁盘的过程.我们知道 InnoDB 使用“Write Ahead Log”策略来避免数据丢失问题,即依靠重做日志来保证 ...
- 创建表结构的sql语句
1.创建表结构 表名: ODS_PSP_DIS_DAY_CALC create table ODS_PSP_DIS_DAY_CALC ( ID CHAR(32) NOT NULL, DIS ...
- python 将mysql数据库中的int类型修改为NULL 报1366错误,解决办法
gt.run_sql()是用pymysql 封装的类 distribution_sort_id type: int目的:将此字段值全部修改为NULL g=2gt.run_sql("updat ...
- python3 安装 basemap 包(windows10)
下载 pyproj 和 basemap 的安装包 basemap 是具有专业标准的地图绘制工具,可以与 matplotlib 的一般绘图功能结合,在地图上绘制数据. basemap 文档:https: ...
- [Hadoop]Hadoop章3 NameNode的ZKFC机制
基本概念 首先我们要明确ZKFC 是什么,有什么作用: zkfc是什么? ZooKeeperFailoverController 它是什么?是Hadoop中通过ZK实现FC功能的一个实用工具. 主要作 ...
- BCode解码练习
在学习BT协议中的一个小练习 参考了 https://github.com/airtrack/bitwave 具体B编码解释 可以自行搜索或者参考 这篇文章 bittorrent 学习(一) 种子文件 ...
- web网站使用qq第三方登录
Html代码: <a href=’/QQlogin’>qq登录</a> //后台代码: @RequestMapping(value = "/QQlogin" ...
- Chapter6_访问权限控制_访问权限修饰词
Java中有四种访问权限,public,private,protected和包访问权限,它们是置于类中每一个成员之前的定义,无论是一个域还是一个方法,下面一一介绍. 一.包访问权限 如果不提供任何访问 ...
- Koa源码分析(一) -- generator
Abstract 本系列是关于Koa框架的文章,目前关注版本是Koa v1.主要分为以下几个方面: 1. Koa源码分析(一) -- generator 2. Koa源码分析(二) -- co的实现 ...