Leetcode -- Container With Most Water

【第一章】基于Simulink的控制器开发教程——目录 摘要:本文系统介绍了基于Simulink的控制器开发方法,涵盖PID、状态反馈、模型预测(MPC)、模糊控制、自适应控制等8类主流控制器。针对每种控制器,详细阐述了其基本原理、适用场景,并通过Simulink建模与仿真测试进行实践验证。文章采用"理论+实践"的结构,从算法设计到硬件实现全流程展开,为汽车、航空航天、工业控制等领域的控制器开发提供完整解决方案,最后通过应用对比与学习总结帮助读者掌握不同控制器的核心特性和应用场景。(149字) 阅读详情

问题链接:https://oj.leetcode.com/problems/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.


API:public int maxArea(int[] height)


问题分析:这一题其实和排序过的Two sum是有相似的地方的,本质上都是dp和贪心算法的体现,但却也有不同之处。

最简单的想法当然就是brute force遍历全部的情况,时间是O(n^2),两层for循环即可。但是这肯定不是最优算法。

下面来分析一下,因为水池的容量是由长度,以及左右之间的短板所决定的。所以根据贪心算法的定义,首先给出两个指针,head = 0, tail = height.length - 1,然后头尾往中间缩。然后替换掉更短的那个板,也就是if(height[head] < height[tail]) head++,反之tail--。 根据这个算法,给出代码如下:

    public int maxArea(int[] height) {
        int head = 0, tail = height.length - 1, res = 0;
        while(head < tail){
            res = Math.max(res, (tail - head) * Math.min(height[head], height[tail]));
            if(height[head] < height[tail])
                head++;
            else
                tail--;
        }
        return res;
    }

关于贪心法,最复杂的并不是算法本身,而是算法适用性的证明,贪心算法比较好用以证明的方式是数学归纳或者反证法,下面给出反证法的说明:

首先给出证明来源,因为我思考了很久但并没有思考出证明的方法.......

https://oj.leetcode.com/discuss/1074/anyone-who-has-a-o-n-algorithm


下面,基于上面的连接,我给出自己的理解好了:

假设存在比我们给出的解更优的解,left_b和right_b,那么根据我们的解法,我们必然会首先到达left_b和right_b的其中一个,假设head先到达left_b好了(反之也一样的,不影响证明的过程),那么我们的tail指针此时还没有到达right_b,接下来根据我们的算法必然会出现两种情形的其中一种

1.tail一路向北,头也不回的就准备和我们的head汇合了。但此时肯定经过了right_b,如果left_b和right_b是更优解,那么必然也会被我们的算法找到。但事实上我们假定了这个解是更优的,所以矛盾了。

2. 在我们的算法里,tail到了某个点,假设是right_c,right_c > right_b,head就离开了我们的最优解left_b,这种情况是唯一有可能我们算法会错过最优解的合理解释。但这种方式也是行不通的,理由如下,首先,不用质疑,right_c - left_b 》 right_b - left_b,所以在长度上占优,而且我们还可以看到min(heights[left_b], heights[right_b]) <= min(heights[left_b], heights[right_c])。理由有点绕,但事实上是这样的:

首先head会离开left_b,是因为height[left_b] < height[right_c] 不满足而诞生的,所以min(heights[left_b], heights[right_c]) = heights[left_b]

而事实上,heights[right_b]此时就没有任何意义了,即使heights[right_b]为任意值,短板min(height[left_b], heights[right_b])也只能小于等于heights[left_b]

因此,left_b和right_b也不可能是最优解,和假设矛盾。


至此,贪心法便得证了。

2023最新多文明策略手游《万国觉醒内购版》最新整理Linux商业手工服务端+安卓+详细搭建教程 2023最新多文明策略手游《万国觉醒内购版》最新整理Linux商业手工服务端+安卓+详细搭建教程。源码内置详细搭建教程,感兴趣可自行下载。 阅读详情

相关推荐

机器视觉:9点标定的原理与实现

标定就是将机器视觉处理得到的像素坐标转换成实际项目中使用到的毫米坐标。简单说即使看看实际单位距离内有几个像素,如下图所示,10mm的距离内有222个像素,那像素坐标和实际的毫米坐标就有个比例关系了。

m0_49968063的博客 1万+

【LeetCode】解题11:Container With Most Water

LeetCode解题 11:Container With Most Water Problem 11: Container With Most Water [Medium]解题思路1. 暴力求解2. 双指针Solution (Java) Problem 11: Container With Most Water [Medium] Given n non-negative integers a1, ...

Fayedily的博客 250

NVIDIA 2025 Board Design Engineer 校招笔试全解析:从基础门电路到理想放大器设计

本文详细解析了NVIDIA 2025届板级设计工程师校招笔试的核心考点与解题策略。内容涵盖从基础门电路、CMOS实现NOR门,到开关电源CCM/DCM模式波形分析与效率计算,以及理想运放电路设计与稳定性优化。旨在帮助考生构建硬件知识体系,掌握工程思维,顺利通过NVIDIA Board Design Engineer校招笔试。

weixin_29172201的博客 196

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 linei is at (i,ai) and (i, 0). Find

漫漫学习路 371

EDF Browser使用说明和技巧

edf browser使用说明和一些小技巧

Erhu_666的博客 8327

STM32实战指南:TFTLCD液晶屏驱动与ILI9341应用解析

本文详细解析了STM32驱动TFTLCD液晶屏的实战技巧,重点介绍了ILI9341控制器的硬件连接、寄存器操作及性能优化方法。通过8080并行接口时序分析、GRAM操作优化和DMA传输应用,帮助开发者高效实现液晶屏驱动,适用于工业控制、智能家居等场景。

weixin_26873711的博客 232

leetcode题目11. Container With Most Water

leetcode题目11. Container With Most WaterNote:Example:CODE: #题目简介 Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that ...

zydapangzi的博客 209

微机原理与接口技术第六版周荷琴课后习题答案及解析(期末重点)

第二章 1.8086/8088CPU可直接寻址多少个内存(字节)单元?多少个I/O端口?它们的外部数据总线各有多少根? 8086/8088CPU使用的是20位地址总线,因此可以直接寻址2的20次方个内存单元(即1,048,576),每个单元大小为1字节,因此可以直接寻址2的20次方个字节的内存。 8086/8088CPU有64K个I/O端口,每个端口占用1字节的地址空间,因此可以直接寻址6...

Wmicheng的博客 1万+

【LeetCode】11. Container With Most Water 解题报告

转载请注明出处:http://blog.youkuaiyun.com/crazy1235/article/details/51541984Subject 出处:https://leetcode.com/problems/container-with-most-water/ Given n non-negative integers a1, a2, …, an, where each represents

crazy_jack 6048

GEO的工作原理,以及GEO会催生出更多的爬虫岗位吗?

摘要: GEO(生成式引擎优化)是提升内容在AI搜索(如ChatGPT、Perplexity等)中引用率的新策略,核心在于优化内容的可抓取性、语义清晰度、权威性。与传统SEO不同,GEO聚焦AI的引用逻辑,需通过爬虫监控AI回答、逆向分析引用源特征(如结构化数据、权威背书),并构建自动化工具优化内容。爬虫工程师需转型为“AI可见性基础设施构建者”,重点关注高权威站点、知识库及官方信息源,同时确保技术可抓取性(如放行AI爬虫UA)。GEO的核心是从“排名”转向“被AI引用”,强调实体识别与动态对话适配。

数据知道的博客 4641

鸿蒙开发实战项目(四十七):图片编辑(ArkTS)

本篇Codelab主要介绍如何基于图片编解码,实现图片编辑。图片的解码。使用PixelMap进行图片编辑,如裁剪、旋转、亮度、透明度、饱和度等。图片的编码。

斌擎科技 1584

Leetcode 11. Container With Most Water

题目描述:找出两个柱子,使其两根柱子面积最大,就是找两个数,使两个面积最大,就是宽*高最大。 题目链接:Leetcode 11. Container With Most Water 子串、子数组的题目,暴力求解,列举子集之后就可以得出结果,不断更新。 还有一个C++的解法那就是左右指针,如何向左向右条件如下。 代码如下 class Solution { public int maxA...

Za七杂⑧ 204

Dify 第 14 篇 Dify 知识库改造案例,如何返回完整文档?

在使用Dify社区版v1.7.1的知识库功能时,遇到检索仅返回匹配的文本片段而非完整文档的问题。针对运维案例知识库的需求,尝试了三种解决方案: 调整Chunk大小:通过设置最大分段长度为4000字符,但无法满足超过该长度的文档需求。 增加返回Chunk数量:通过调整Top K参数,但最大仅支持10个片段,且可能包含无关内容。 调用知识库API:通过检索接口获取匹配文档ID,再查询该文档所有分段内容,从而获得完整信息。具体步骤包括生成API密钥、获取知识库ID,并通过HTTP请求实现。 最终推荐采用AP

甘蓝的专栏 1969

[LeetCode] 011. Container With Most Water (Medium) (C++/Java/Python)

[LeetCode] 011. Container With Most Water (Medium) (C++/Java/Python)

水果君の日常 2386

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

Leetcode11:Container With Most Water

Leetcode11 Container With Most Water Container With Most Water The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container ...

qq_24367757的博客 189

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

leetcode011 Container With Most Water

题目11. 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

jnuyanfa的博客 356

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

Leetcode 11 Container With Most Water

Leetcode 11 Container With Most Water class Solution{ public: int maxArea(vector&lt;int&gt;&amp; height){ int left = 0; int right = height.size()-1; int max_area = 0; while(right &g...

自己的学习笔记 201

leetcode记录11.Container With Most Water

leetcode记录11.Container With Most Water # # @lc app=leetcode id=11 lang=python3 # # [11] Container With Most Water # # https://leetcode.com/problems/container-with-most-water/description/ # # algorithm...

我的博客 214

LeetCode | Container With Most Water

11. Container With Most Water Description 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 o...

Karmen199x的博客 151

leetcode解题报告11. Container With Most Water

leetcode解题报告11. Container With Most Water题目地址 难度是medium题目描述原文: Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that th

sysu_xiaoming的博客 274

LeetCode11. Container With Most Water

11. Container With Most Water 1、原题 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

u010217031的博客 323

[leetcode]11. Container With Most Water

[leetcode]11. Container With Most Water Analysis 是阴天呢—— [每天刷题并不难0.0] Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn s...

weixin_32135877的博客 145

LeetCode:11. Container With Most Water

LeetCode:11. 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 li...

Wisimer 217

Leetcode1-50: 11. Container With Most Water

Leetcode1-50: 11. Regular Expression Matching问题描述解题思路Brute force线性算法代码实现implement 1 Brute forceimplement 2 线性算法复杂度分析分析 问题描述 Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such t

Y123iwantit的博客 232

Container With Most Water - LeetCode

Container With Most Water - LeetCode 题目: 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 endp

不要说话的博客 696

Leetcode - 11 - 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 the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a cont.

xin_rong的博客 147
上一篇: Leetcode -- Regular Expression Matching
下一篇: Leetcode -- Longest Common Prefix
Daisy么么哒
博客等级 码龄12年 10粉丝 310原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值