当前位置:网站首页>11. Container with the most water
11. Container with the most water
2022-07-20 05:21:00 【Xiao Lu wants to brush the questions】
List of articles
Preface
Given a length of n Array of integers for height . Yes n Vertical line , The first i The two ends of the line are (i, 0) and (i, height[i]) .
Find two of them , Make them x A container of shafts can hold the most water .
Return the maximum amount of water that the container can store .
explain : You can't tilt the container .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/container-with-most-water
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
One 、 Their thinking
Assume no duplicate values first , The left and right pointers are bigger than the right , Who accounts for the amount of water
The answer is to calculate the maximum value of all water volume in the process of drawing two left and right pointers in turn
It is not strictly required that every number can accurately calculate its answer
It only cares about the possibility of pushing up the answer
Why don't you know how to do this problem ? But look at the amount of data , You know you'll make one O(N) Solution , It is bound to think of double pointers , Whoever moves a lot and who moves a little will try this problem again , Don't know why it's possible to guess
Pictured , Move the small pointer , Water volume per calculation , Don't miss the answer
Just focus on the possibility that it pushes up the answer , But we don't strictly entangle the value of each position , What is the specific answer
Code
class Solution {
public int maxArea(int[] height) {
int l=0;
int r=height.length-1;
int max=0;
while(l<r){
max=Math.max(max,Math.min(height[l],height[r])*(r-l));
if(height[l]>height[r]){
r--;
}else{
l++;
}
}
return max;
}
}
summary
Array problem encountered , And the data volume of the topic requires the use of O(n) Solution , We can consider using double pointers
We can compare the number of two pointers , Think about how to move the pointer
In this question , We found that whoever moves a little can get the answer
边栏推荐
- 络达开发--SideTone配置的來龍去脈
- 技术解析|Doris Connector 结合 Flink CDC 实现 MySQL 分库分表 Exactly Once精准接入
- 电脑系统还原有什么后果和问题
- 值得一看的智能运维AIOps关键核心技术概览!
- 分享|2022数字安全产业大数据白皮书(附PDF)
- Apache Hudi数据跳过技术加速查询高达50倍
- Embedded interview questions
- cdh配置外部mysql([main] DbCommandExecutor ERROR Error when connecting to database.)
- Servlet Error instantiating servlet class NoSuchMethodException
- TOGAF中的本手和妙手
猜你喜欢
递归实现排列型枚举
爱可可AI前沿推介(7.18)
Case analysis of building enterprise finance and tax service system with low code
Lookup is used in comparison with vlookup; Sumifs vs. sumproduct
分享|2022数字安全产业大数据白皮书(附PDF)
可落地的DDD(6)-工程结构
Pinecone trip x starrocks: the practical road of the new paradigm of real-time data warehouse
TIA博途S7-1200中实现高低字节或高低字调换的几种方法介绍
TOGAF中的本手和妙手
卷积核扩大到51x51,新型CNN架构SLaK反击Transformer
随机推荐
方文档。最API。基于这套Aage,昇腾
Information system project managers must recite the core examination points (45) Bidding Law
Flink实时仓库-ODS&DIM层实现模板代码
ArkUI框架进度指示器
Acwing daily question 3715 Minimum exchange times
[icml2022] gnnrank: learning global ranking from pairwise comparison based on directed graph neural network
Day24作业
MySQL --- 多表查询 - 笛卡尔积和正确的多表查询、等值连接和不等值连接、内连接和外连接
scroll-view的下拉刷新和上拉加载(触底)
记一次Spark foreachPartition导致OOM
分布式锁的实现
ACM MM 2022 | 面向视频文本检索的端到端多粒度对比学习
盘点云计算的概念,分类和特点
rust求两数之和
lookup与VLOOKUP对比使用;sumifs与sumproduct对比使用
值得一看的智能运维AIOps关键核心技术概览!
信息系统项目管理师必背核心考点(四十五)招标投标法
微服务架构 | 消息队列 - [常见坑] TBC...
动手实践丨手把手教你用STM32做一个智能鱼缸
微服务架构 | 消息总线和驱动 - [Bus & Stream]