当前位置:网站首页>Graphic LeetCode -- 11. Containers of most water (difficulty: medium)
Graphic LeetCode -- 11. Containers of most water (difficulty: medium)
2022-07-30 18:07:00 【Java Muse】
一、题目
给定一个长度为 n 的整数数组 height .有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) .
找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水,返回容器可以储存的最大水量.
说明:你不能倾斜容器.
二、示例
2.1> 示例 1:

【输入】[1,8,6,2,5,4,8,3,7]
【输出】49
【解释】图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7].在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49.
2.2> 示例 2:
【输入】height = [1,1]
【输出】1
提示:
- n == height.length
- 2 <= n <= 105
- 0 <= height[i] <= 104
三、解题思路
3.1> 思路1:双向指针
通过题意,We will receive an array of integersheight,它里面有n个数值,represents the height of the stake.Any two stakes can be combined withx轴Composed of a container to sink,So which combination sink can hold more water??In fact, there are two factors in this question that are the key points of the water capacity:one is a sinkx轴的长度,The other is the height of the shortest stake of the two stakes.The product of these two values is the size of the water holding capacity..
Then the solution to the two-way pointer is to create two pointers——head头指针和tail尾指针.最初head指向height数组中index=0的位置,tail指向height数组中index=height-1的位置.So at this time, suppose we are as shown in the following figure,height[head]是小于height[tail]的,head指针与tailThe length of the pointer isx(即:tail - head = height - 1 - 0),Then the water capacity that the sink can carry at this time is equal to:height[head] * x.如下图所示:

Then according to the double pointer arithmetic,我们需要让head指针或者tailpointers move towards each other,So which pointer to move?We will compare the heights of the two stakes according to,to move the shorter one.比如,headThe stake pointed to by the pointer is shorter,那么我们将head指针向右移动.但是如果是tailThe stake pointed to by the pointer is shorter,那么我们就将tail指针向左移动.When two Pointers met in one location,则结束整个过程.
那么有同学会问,Why make such a comparison??这道题,我们一般来说,The first reaction should be two layersfor循环,即,To lock the first wooden stakes,Then go to compare other stakes in turn,capacity per calculation,通过Math的max函数,Only record the maximum capacity.Then we lock the second stake,Compare with other stakes(not the first stake),然后一次类推.This way is right,But it belongs to the category of brute force.那么,Can a two-way pointer guarantee the correctness of the calculation??
Let's take the above picture as an example,由于最初的headThe height of the pointed stake isheight[0],tailThe height of the pointed stake isheight[height.length - 1],他们之间的距离为x,由于height[0] 小于 height[height.length - 1],So the water capacity of the container is height[0] * x,这里的xalready at the maximum,也就是说,无论head指针和tailhow the pointer moves,The distance between the two pointers will not be greater thanx.Then we actually相当于“锁定”max container bottomx了,And every time you movehead指针还是tail指针,for the bottom of the containerx的影响,are always decreasing,Then its change is a constant decreasing trend.And in terms of container height,indeed a change,Because we don't know whatheadpointed stake high,还是tailpointed stake high.
那么此时,Our second question is,How to move the stake?Why move the shorter one??By the above formula we getheight[head] * x(前提是headThe height of the stake is less thantailstake height),那么假设我们Move higher stakes——即tail指针,Then the bottom of the container is:x - 1,Since the shorter stakes are not movedheight[head],所以无论tailWhat is the height of the stake pointed to by the pointer?,The total capacity height will not exceedheight[head]的高度,Then the maximum possible capacity is:height[head] * (x - 1);Then if weMove lower stakes——即head指针,Then the bottom of the container is:x - 1,Since the higher stakes are not movedheight[tail],所以无论headWhat is the height of the stake pointed to by the pointer?,The total capacity height will not exceedheight[tail]的高度,Then the maximum possible capacity is:height[tail] * (x - 1);那么由于head < tail,So naturallyheight[head] * (x - 1) <height[tail] * (x - 1)的结论.And this question is to get the maximum water capacity,所以,Nature is about to move to a stake with shorter.具体如下图所示:

Then we have introduced how to calculate the maximum water holding capacity above.,The following is the first example in the title1为例,Graphical way of performing through each step,Completely show the whole process of one execution.那么示例1中,height数组中的元素为[1,8,6,2,5,4,8,3,7],We in turn move throughhead指针或者tail指针,and calculate each“最大容量” ,并通过Math的max函数,Return the final maximum capacity as the result.下面是具体的8个执行步骤,如下所示:


四、代码实现
4.1> 实现1:双向指针
class Solution {
public int maxArea(int[] height) {
int result = 0, head = 0, tail = height.length - 1;
while(head < tail) {
if (height[head] <= height[tail]) {
result = Math.max(height[head] * (tail - head), result);
head++;
} else {
result = Math.max(height[tail] * (tail - head), result);
tail--;
}
}
return result;
}
}
今天的文章内容就这些了:
写作不易,笔者几个小时甚至数天完成的一篇文章,只愿换来您几秒钟的 点赞 & 分享 .
更多技术干货,欢迎大家关注公众号“爪哇缪斯” ~ (^o^)/ ~ 「干货分享,每天更新」
往期推荐
题目来源:力扣(LeetCode)
边栏推荐
- LayaBox---TypeScript---泛型
- DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计
- What is an ultrasonic flaw detector used for?
- Leetcode数据库系列题解合集(持续更新)
- X射线的应用是什么?
- CMake库搜索函数居然不搜索LD_LIBRARY_PATH
- Network Basics (3) 01-Basic Concepts of Networks - Protocols, Host Addresses, Paths and Parameters of URL Addresses & 127.0.0.1 Local Loopback Address & View URL IP Address and Access Ping Space + URL
- LeetCode 952. 按公因数计算最大组件大小
- JVM诊断命令jcmd介绍
- The sixteenth issue of eight-part article Balabala said (MQ)
猜你喜欢

莫队--优雅的暴力

Web 3.0入门教程

Application of time series database in the field of ship risk management

今年这情况。。真心推荐专科的工程师升个本!

载誉而归,重磅发布!润和软件亮相2022开放原子全球开源峰会

Quickly build an e-commerce platform based on Amazon cloud technology serverless service - performance

5 个开源的 Rust Web 开发框架,你选择哪个?

开源盛宴ApacheCon Asia 2022即将开幕,精彩不容错过!

Kettle--MySQL生产数据库千万、亿级数据量迁移方案及性能优化

图解LeetCode——11. 盛最多水的容器(难度:中等)
随机推荐
宽带射频放大器OA4SMM4(1)
【网络工程】A、B、C、D、E类IP地址划分依据和特殊的IP地址
微博广告分布式配置中心的构建与实践(有彩蛋)
leetcode-684:冗余连接
网络基础(三)01-网络的基础概念——URL地址组成之协议、主机地址、路径和参数&127.0.0.1本地回环地址& 查看网址IP地址并访问之ping空格+网址&netstat -anb查看本机占用端口
如何让 JOIN 跑得更快?
【HarmonyOS】【FAQ】鸿蒙问题合集4
测试.net文字转语音模块System.Speech
基于亚马逊云科技无服务器服务快速搭建电商平台——性能篇
PLSQL Developer安装和配置
Pagoda builds PHP adaptive lazy website navigation source code measurement
CMake library search function does not search LD_LIBRARY_PATH
首发!阿里技术大牛最新耗时半个月整理出最全MySQL性能优化和高可用架构技术宝典,直接封神!
X射线的应用是什么?
OSPF详解(3)
Linux-安装MySQL(详细教程)
银行适用:此文能够突破你的运维流程管理问题
Mo Team - Elegant Violence
What is NDT equipment?
linux 下MySQL本地安装mysql - u root - p 无法登入