弟中弟的Leetcode总结——数组类(二)
弟中弟的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总结——数组类(二)相关推荐
- 弟中弟的Leetcode总结——数组类(十一)
弟中弟的Leetcode总结--数组类(十一) 题目描述 Single Number Given a non-empty array of integers, every element appear ...
- 弟中弟的Leetcode总结——数组类(十)
弟中弟的Leetcode总结--数组类(十) 题目描述 Climbing Stairs You are climbing a stair case. It takes n steps to reach ...
- 弟中弟的Leetcode总结——数组类(五)
弟中弟的Leetcode总结--数组类(五) 题目描述 Remove Duplicates from Sorted Array Given a sorted array nums, remove th ...
- 弟中弟的Leetcode总结——数组类(四)
弟中弟的Leetcode总结--数组类(四) 题目描述 Two Sum Given an array of integers, return indices of the two numbers su ...
- 弟中弟的Leetcode总结——数组类(八)
弟中弟的Leetcode总结--数组类(八) 题目描述 Combination Sum Given a set of candidate numbers (candidates) (without d ...
- 弟中弟的Leetcode总结——数组类(九)
弟中弟的Leetcode总结--数组类(九) 题目描述 Subsets Given a set of distinct integers, nums, return all possible subs ...
- 弟中弟的Leetcode总结——数组类(六)
弟中弟的Leetcode总结--数组类(六) 题目描述 3Sum Closest Given an array nums of n integers and an integer target, fi ...
- 弟中弟的Leetcode总结——数组类(七)
弟中弟的Leetcode总结--数组类(七) 题目描述 Maximum Subarray Given an integer array nums, find the contiguous subarr ...
- 弟中弟的Leetcode总结——字符串类(一)
弟中弟的Leetcode总结--字符串类(一) 题目描述 Longest Substring Given a string, find the length of the longest substr ...
最新文章
- centos yum mysql-devel 5.5_CentOS 6.5下yum安装 MySQL-5.5全过程图文教程
- 使用RMAN在ASM和文件系统之间拷贝数据
- c# List 按类的指定字段排序
- c语言不被O2优化,【图片】今天写几个性能测试,为什么C语言跑得这么慢呢??【c语言吧】_百度贴吧...
- 程序员如何理解Gmail“撤销发送”功能
- 怎么画闭合线_骨骺线闭合怎么长高
- msp430和stm32 lcd屏驱动程序
- iphone在jsp显示时间会NAN解决办法
- Lingo线性规划教程
- 自定义设置软件的提示声音
- DLL注入——使用注册表
- 我从零开始学黑莓开发的过程
- 公司要一个网站,是选模板建站还是定制化建站?
- ai钢笔工具怎么描线_ps钢笔工具怎么用——绘制直线、平滑曲线、转折曲线
- 个性化推荐系统设计(4.1)——案例分析
- cf C. Counting Kangaroos is Fun
- 2018年人工智能领域研发热点回眸
- has leaked window com.android.internal.policy.impl.PhoneWindow解决(Dialog.cancel、dismiss、hide区别)
- Blender - 法向 缩放 , Shrink Fatten 应用
- LVS均衡负载(一) LVS详解
热门文章
- Linux 多线程 ”一写多读” 模式下的无锁设计
- Adobe Illustrator(Ai) 2020中文版
- 圆管压降计算(利用达西公式)
- jQuery实现文件上传大小限制
- 运营必备的 15 个数据分析方法(论)
- 在python官网如何下载手机版_Python教学免费下载-Python教学appv1.0 官方版-腾牛安卓网...
- 西门子300系列基本逻辑编程:手自动选择程序及自定义脉冲模块的使用
- 怎么将word文档转为pdf
- mysql abb复制_abb变频器怎样复制数据
- 第一章——概率论基本概念