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 ...
随机推荐
- JavaScript进度条(datalist/repeater等多个进度条)
JS代码: function SingleProgressBar() { var iload = document.getElementById("iLoading"); var ...
- redis+Keepalived主从热备切换实例
[root@cache-redis-~/]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived globa ...
- jq动画设置图片抽奖(修改效果版)
效果:点击开始,图片转动,按钮显示"停止",点击停止,选出中奖号码,,最后一个数字停止时,按钮变为"again",点击"again"开始转动 ...
- Celery 1
Celery是一个用Python开发的异步的分布式任务调度模块 Celery有以下优点: 简单:一但熟悉了celery的工作流程后,配置和使用还是比较简单的 高可用:当任务执行失败或执行过程中发生连接 ...
- vim常用配置 vimrc文件
自从接触vim,自己瞎鼓捣.vimrc也有一段时间了.收集记录一下好用的配置. 一.奇技淫巧 1.折叠代码 折叠代码常常用在代码块较长的情况下,比如一个文件里定义了很多个函数,或者注释.括号影响的阅读 ...
- 【c # 数据库】存储过程
可理解存储过程是方法,快速调用,方便使用. 数据库建立新的存储过程: CREATE PROCEDURE myProc -- Add the parameters for the stored proc ...
- TZOJ 2569 Wooden Fence(凸包求周长)
描述 Did you ever wonder what happens to your money when you deposit them to a bank account? All banks ...
- android 7.0+ FileProvider 访问隐私文件 相册、相机、安装应用的适配
从 Android 7.0 开始,Android SDK 中的 StrictMode 策略禁止开发人员在应用外部公开 file:// URI.具体表现为,当我们在应用中使用包含 file:// URI ...
- beautiful模块
- crm开发之用户ModelForm定制和密码加密
写了这么多的定制 功能.终于可以定制一下了!因为是 stark 和 rbac 两个组建. 一起使用. 所以在这里,再记录一下.需要注意的点: 先放出 目录结构: 先从 stark 开始.使用star ...