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

本文共 899 字,大约阅读时间需要 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 and n is at least 2.

example

Example:

Input: [1,8,6,2,5,4,8,3,7]

Output: 49

我的思路

使用双指针,一个从头一个从尾,但是具体怎么控制指针的移动?谁大移动谁?有点想法,但是很模糊

解答

感觉已经跟答案的想法很接近了!其实自己再多梳理一下就能做出来了

这里的思想是,因为往内移动,长是越来越短的,所以需要移动指针去寻找更高的地方

public class Solution {
public int maxArea(int[] height) {
int maxarea = 0, l = 0, r = height.length - 1; while (l < r) {
maxarea = Math.max(maxarea, Math.min(height[l], height[r]) * (r - l)); if (height[l] < height[r]) l++; else r--; } return maxarea; }}

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

你可能感兴趣的文章
ORA-00258: NOARCHIVELOG 模式下的人工存档必须标识日志
查看>>
Java调用bat文件
查看>>
此责任无可用函数
查看>>
java获取数字和汉字
查看>>
excel Option Explicit webadi
查看>>
ICX错误
查看>>
windows Xp NTLDR is missing
查看>>
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
查看>>
Centos 6.x 安装配置MySQL
查看>>
-source 1.5 中不支持 diamond 运算 请使用 -source 7 或更高版本以启用
查看>>
jar包读取资源文件报错:找不到资源文件(No such file or directory)
查看>>
超简单:Linux安装rar/unrar工具与解压到目录示例
查看>>
Eclipse创建Maven Java8 Web项目,并直接部署Tomcat
查看>>
RedHad 7.x服务器操作记录
查看>>
BindException: Cannot assign requested address (Bind failed)解决办法
查看>>
Centos7:Docker安装Gitlab
查看>>
Kafka日志配置
查看>>
logstash 6.x 收集syslog日志
查看>>
Apche Kylin启动报错:UnknownHostException: node1:2181: invalid IPv6 address
查看>>
Apache Kylin 2.3 构建Cube失败
查看>>