弟中弟的Leetcode总结——数组类(二)

题目描述

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

思路——Naive Solution

最简单的方法就是暴力枚举,从第一个开始一直到最后一个,算他们的乘积。

代码(py3)

class Solution:def maxArea(self, height):""":type height: List[int]:rtype: int"""leng=len(height)maxv=0for i in range(leng):for j in range(i+1,leng):val=min(height[i],height[j])*(j-i)maxv=val if(val>maxv) else maxvreturn maxv

思路——Improved Solution

前面的解法时间为O(nlog(n)),显然会超时,因此需要对算法进行改进。
之前的算法是从左到右逐一扫描,对于每一个数,都需要扫描(n-i)遍。但是在这之中忽略了给定的数值的作用。显然如果数值越高,
那么能装的也就越多。因此可以设定两个指针,开始的时候指向左右两端,然后逐渐向中间移动,每次移动的时候移动数值较小的那个。

代码(py3)

class Solution:def maxArea(self, height):""":type height: List[int]:rtype: int"""leng=len(height)maxv=0i=0j=leng-1while(i!=j):val=min(height[i],height[j])*(j-i)maxv=val if(val>=maxv) else maxvif(height[i]<height[j]):i+=1else:j-=1return maxv

弟中弟的Leetcode总结——数组类(二)相关推荐

  1. 弟中弟的Leetcode总结——数组类(十一)

    弟中弟的Leetcode总结--数组类(十一) 题目描述 Single Number Given a non-empty array of integers, every element appear ...

  2. 弟中弟的Leetcode总结——数组类(十)

    弟中弟的Leetcode总结--数组类(十) 题目描述 Climbing Stairs You are climbing a stair case. It takes n steps to reach ...

  3. 弟中弟的Leetcode总结——数组类(五)

    弟中弟的Leetcode总结--数组类(五) 题目描述 Remove Duplicates from Sorted Array Given a sorted array nums, remove th ...

  4. 弟中弟的Leetcode总结——数组类(四)

    弟中弟的Leetcode总结--数组类(四) 题目描述 Two Sum Given an array of integers, return indices of the two numbers su ...

  5. 弟中弟的Leetcode总结——数组类(八)

    弟中弟的Leetcode总结--数组类(八) 题目描述 Combination Sum Given a set of candidate numbers (candidates) (without d ...

  6. 弟中弟的Leetcode总结——数组类(九)

    弟中弟的Leetcode总结--数组类(九) 题目描述 Subsets Given a set of distinct integers, nums, return all possible subs ...

  7. 弟中弟的Leetcode总结——数组类(六)

    弟中弟的Leetcode总结--数组类(六) 题目描述 3Sum Closest Given an array nums of n integers and an integer target, fi ...

  8. 弟中弟的Leetcode总结——数组类(七)

    弟中弟的Leetcode总结--数组类(七) 题目描述 Maximum Subarray Given an integer array nums, find the contiguous subarr ...

  9. 弟中弟的Leetcode总结——字符串类(一)

    弟中弟的Leetcode总结--字符串类(一) 题目描述 Longest Substring Given a string, find the length of the longest substr ...

最新文章

  1. centos yum mysql-devel 5.5_CentOS 6.5下yum安装 MySQL-5.5全过程图文教程
  2. 使用RMAN在ASM和文件系统之间拷贝数据
  3. c# List 按类的指定字段排序
  4. c语言不被O2优化,【图片】今天写几个性能测试,为什么C语言跑得这么慢呢??【c语言吧】_百度贴吧...
  5. 程序员如何理解Gmail“撤销发送”功能
  6. 怎么画闭合线_骨骺线闭合怎么长高
  7. msp430和stm32 lcd屏驱动程序
  8. iphone在jsp显示时间会NAN解决办法
  9. Lingo线性规划教程
  10. 自定义设置软件的提示声音
  11. DLL注入——使用注册表
  12. 我从零开始学黑莓开发的过程
  13. 公司要一个网站,是选模板建站还是定制化建站?
  14. ai钢笔工具怎么描线_ps钢笔工具怎么用——绘制直线、平滑曲线、转折曲线
  15. 个性化推荐系统设计(4.1)——案例分析
  16. cf C. Counting Kangaroos is Fun
  17. 2018年人工智能领域研发热点回眸
  18. has leaked window com.android.internal.policy.impl.PhoneWindow解决(Dialog.cancel、dismiss、hide区别)
  19. Blender - 法向 缩放 , Shrink Fatten 应用
  20. LVS均衡负载(一) LVS详解

热门文章

  1. Linux 多线程 ”一写多读” 模式下的无锁设计
  2. Adobe Illustrator(Ai) 2020中文版
  3. 圆管压降计算(利用达西公式)
  4. jQuery实现文件上传大小限制
  5. 运营必备的 15 个数据分析方法(论)
  6. 在python官网如何下载手机版_Python教学免费下载-Python教学appv1.0 官方版-腾牛安卓网...
  7. 西门子300系列基本逻辑编程:手自动选择程序及自定义脉冲模块的使用
  8. 怎么将word文档转为pdf
  9. mysql abb复制_abb变频器怎样复制数据
  10. 第一章——概率论基本概念