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 ...
随机推荐
- Linux - 用户权限相关命令
用户权限相关命令 目标 用户 和 权限 的基本概念 用户管理 终端命令 组管理 终端命令 修改权限 终端命令 01. 用户 和 权限 的基本概念 1.1 基本概念 用户 是 Linux 系统工作中重要 ...
- 关于textarea标签自己遇到的问题
效果描述: 固定文本框的宽高,让文本框不被拖拽,让提示内容的文字垂直居中 html: <div> <label>活动详情</label> <textarea ...
- nginx+lua+kafka 编写 在线日志上报系统
案例一 rewrite_by_lua ' --引入openresty自带的json处理对象 local cjson = require("cjson") local produce ...
- excel 上传读写到数据库
<HTML> <div class="input-group"> <form id="abc" action="http ...
- 在windows下使用jenkins部署docker容器
在windows下使用jenkins部署docker容器最近在学习jenkins,docker部署来实现集成部署,所以想在windows下面实现测试,但是发现在windows下docker支持不是很好 ...
- grid布局
display: grid; //行 //每一行中有几块,每块所占的width的百分比(1) grid-template-columns: 70% 30%; //每个div的width 100px,自 ...
- js计算日期相减天数
代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head ...
- django 富文本编辑器
创建工程,数据中数据格式设置为 models.TextField() 此时编辑器是普通的文本框. 使用 tinymce .安装 django-tinymce pip install django-ti ...
- Python开发——目录
Python基础 Python开发——解释器安装 Python开发——基础 Python开发——变量 Python开发——[选择]语句 Python开发——[循环]语句 Python开发——数据类型[ ...
- 安卓学习 intent
其实学习了好几个星期了,是看老罗的视频,但进度太慢 今天 换了一本书 Intent 切换页面 啊啊啊啊 CompentName comp=new CompentName(MainActivity.th ...