今天开始第一天记录刷题,本人编程小白,如果有写的不对、或者能更完善的地方请个位批评指正!

准备按tag刷,第一个tag是array:

这个是array的第一道题:11. Container With Most Water

Given n non-negative integers a1a2, ..., an , where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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

 
 (From: https://leetcode.com/problems/container-with-most-water/description/)

思路:

  这道题是找:(取两个数组元素中比较小的哪个数字)*两数组元素之间宽度(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)的更多相关文章

  1. LeetCode11 Container With Most Water

    题意: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  2. [LeetCode]Container With Most Water, 解题报告

    前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n no ...

  3. 思维题(两点逼近)LeetCode11 Container with Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  4. [LeetCode] 42. Trapping Rain Water 解题思路

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  5. Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

    本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Po ...

  6. LeetCode Two Sum 解题思路(python)

    问题描述 给定一个整数数组, 返回两个数字的索引, 使两个数字相加为到特定值. 您可以假设每个输入都有一个解决方案, 并且您不能使用相同的元素两次. 方法 1: 蛮力 蛮力方法很简单.循环遍历每个元素 ...

  7. Leetcode11.Container With Most Water盛最多水的容器

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...

  8. leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

  9. 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 ...

随机推荐

  1. scapy基础之一 ----简单命令

    前言 scapy是python写的一个功能强大的交互式数据包处理程序,可用来发送.嗅探.解析和伪造网络数据包,常常被用到网络攻击和测试中.下面介绍简单命令. ls() List all availab ...

  2. BootStrap布局组件

    BootStrap字体图标(Glyphicons) BootStrap下拉菜单:下拉菜单是可以切换的,是以列表格式显示链接的上下文菜单. 类 描述 .dropdown 指定下拉菜单 .dropdown ...

  3. oracle 中查看数据库表中某个字段是否重复

    1.select  表中重复的字段 from  表名 group by 表中的重复的字段 HAVING count(表中的重复的字段)>1 举例说明 : 表名 : psp_cell_model  ...

  4. tomcat关闭时Log4j2报错 Log4j Log4j2-TF-4-Scheduled-1 memory leak

    出错信息: 23-Sep-2017 17:43:18.964 警告 [main] org.apache.catalina.loader.WebappClassLoaderBase.clearRefer ...

  5. 8.Redis内存分配

    8.Redis内存分配8.1 内存消耗8.1.1 内存使用统计8.1.2 内存消耗划分8.1.3 子进程内存消耗8.2 内存管理8.2.1 设置内存上限8.2.2 动态调整内存上限8.2.3 内存回收 ...

  6. java socket编程(一)简介

    #Java TCP Ip编程 其实下面几张图片就可以解释简单解释tcp-ip协议的大体流程了. ###计算机网络,分组报文和协议 网络是一组通过通信信道相互连接的机器组成. 组与组之间通过路由器连接 ...

  7. SqlServer2012,设置指定数据库对指定用户开放权限

    REVOKE VIEW ANY DATABASE TO [public] --这个是取消数据库公开的权限,也就是除了sa角色外任何人都不能查看数据库 -- 现在用sa用户登录Use [要开放权限的数据 ...

  8. iframe和form表单实现ajax请求上传数据

    form的target属性设置为iframe的name值时,表示提交到url后返回的数据显示到iframe区域 <form action="/upload.html" met ...

  9. 获取Vue的实例方法

    我们知道在new Vue({...})后,如果没有赋值给一个变量存储,我们很难拿到这个实例,Vue官方也没有提供Vue.getInstance方法,那我们就自己扩展个吧 Code: Vue.getIn ...

  10. ELK日志系统使用说明

    数据探索 Elasticsearch具有强大的数据检索和分析同能,支持模糊.全文.过滤.管道等数据查询.对于日志型数据处理很有优势. 下图为KIbana的主页图,将逐步说明每一部分的功能: 依照图中的 ...