博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
11. Container With Most Water (python)
阅读量:3733 次
发布时间:2019-05-22

本文共 798 字,大约阅读时间需要 2 分钟。

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.
题意:给定n条线段,第i条线段的两个端点是(i,0)和(i,ai),这n条线段都是垂直于x轴的,选取其中的两条线段,使这两条线段和x轴构成的容器能容纳的水最多。
方法一:两层循环时间复杂度为O(n^2),但肯定会超时
方法二:
参考:
容积即面积,它受长和高的影响,当长度减小时候,高必须增长才有可能提升面积,所以从长度最长时开始递减,然后寻找更高的线来更新候补;
假设我们找到能取最大容积的纵线为 i , j (假定i

class Solution(object):    def maxArea(self, height):        n=len(height)        if n<2:            return 0        i=0  #左边        j=n-1  #右边        con=0  #容积        while  i
i: k-=1 j=k return con

转载地址:http://jcfin.baihongyu.com/

你可能感兴趣的文章
JAVA基础15——方法参数
查看>>
spring boot 实现微信扫码登录
查看>>
内网穿透
查看>>
java微信公众号授权登录
查看>>
java 动态导出excel表单 无模板文件下载
查看>>
java 动态导出excel表单 无模板本地生成
查看>>
spring动态导入无模板excel
查看>>
八大排序之直接插入排序
查看>>
二叉树
查看>>
二叉树
查看>>
入门级Java版学生管理系统
查看>>
javaweb
查看>>
Mybatis-study
查看>>
SpringMVC-study
查看>>
ssmbuild
查看>>
springboot + Mybatis-plus
查看>>
笔记图片
查看>>
记录学习springboot连接阿里云oss使用视频点播
查看>>
使用Java爬取图片
查看>>
Mybatis-puls逆向工程
查看>>