[Leetcode] 11. Container With Most Water

PCA9698嵌入式C++驱动库:高性能I²C GPIO扩展方案 GPIO扩展是嵌入式系统中解决MCU引脚资源不足的关键技术,其核心原理在于通过串行总线(如I²C)桥接主控与外设寄存器,实现逻辑引脚到物理IO的映射与状态同步。技术价值体现在低开销、高确定性与多设备可扩展性,广泛应用于工业控制、传感器节点和LED矩阵驱动等实时场景。本文聚焦PCA9698——一款支持24通道双向IO、中断输出与可编程驱动模式的I²C GPIO扩展芯片,结合其C++驱动库的影子寄存器机制、端口级原子操作及非阻塞同步设计,提供面向资源受限平台(如ATmega2560)的工程就绪方案。 阅读详情

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 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.

Language: C++

解法一:Time Limit Exceeded
暴力解法,遍历所有i,j可能的组合,然后依次判断是否结果大于当前最大容量volume。对于数据量庞大(如15000)时超时Time Limit Exceeded不能AC。
Time: O(n2)O(n2)
Space: O(1)O(1)

class Solution {
public:
    int maxArea(vector<int>& height) {

        assert(height.size() > 1);

        int maxwater = 0, i = 0, j = height.size()-1;

        for(int i = 0; i < height.size()-1; i++){
            for(int j = i+1; j < height.size(); j++){

                int h = height[i] > height[j] ? height[j] : height[i];

                if(h * (j - i) > maxwater)
                    maxwater = h * (j - i);
           }
        }
        return maxwater;
    }
};

解法二:AC
基本思想:对撞指针。
思路:这道题是可以用对撞指针去做的,不确定的话就画个图试试。问题要求的是最大的volume,如果不好理解,可以先画个图,看看具体是怎么变动的。i从头开始,j从尾开始,比较height[i]和height[j],较小的那个(短板)决定了我们能装多少水,此时高的板再高也没有用,水量已经被短板决定,而我们是为了找最大的volume,所以继续找一个板大于当前短板。
如height[i] < height[j], 此时挪动j只可能找到比当前水量更少的水量(如果j板更大或相等,最小高度都是height[i]不变,由于距离更近了,此时只可能更小; 如果找到一个比height[i]更小的板,那么只可能更小了),因此我们要挪动的是i,使得另一个height[i]大于当前height[i],来得到更多的水量。

另:liuyubobobo老师的解答:
“我们可以形象的理解一下初始的情况是底边最长的情况在这样的情况下如果存在容积更大的容器底边肯定会缩小。此时唯一的方法就是增加高度。我们每次都尝试舍弃两边最短的那个高度来看新的高度能不能构建出容积更大的容器出来”

         |           |
         |  |     |  |     |
      |  |  |  |  |  |  |  |
      ----------------------
start i                    j  
         i                 j
         i              j
         i           j
         i        j    
         i     j
         i  j
 end     ij      

Time: O(n)O(n)
Space: O(1)O(1)

class Solution {
public:
    int maxArea(vector<int>& height) {

        assert(height.size() > 1);

        int maxwater = 0, i = 0, j = height.size()-1;

        while(i < j){

             int h = height[i] > height[j] ? height[j] : height[i];      
             if(h * (j - i) > maxwater)
                maxwater = h * (j - i);

            if(height[i] < height[j])
                i++;
            else
                j--;           
        }      
        return maxwater;
    }
};
Rocky Linux 9.0国内yum源一键替换指南(上海交大镜像站实测) 本文详细介绍了如何将Rocky Linux 9.0的yum源替换为上海交大镜像站,以解决国内用户访问官方源速度慢的问题。通过实测验证,上海交大镜像站提供高速下载和稳定更新,并提供了完整的替换步骤、常见问题解决方案及高级优化配置,帮助用户提升软件包下载效率。 阅读详情

相关推荐

从理论到板级:FOC电机驱动硬件电路全链路设计解析

本文从硬件工程师视角,深度解析FOC电机驱动从理论到板级实现的全链路设计。重点剖析了电源保护、核心采样电路、三相逆变桥与驱动电路等关键硬件模块的设计要点与实战经验,强调硬件是FOC算法可靠运行的基石,并提供了PCB布局布线的核心原则,帮助开发者构建高性能、高可靠性的电机驱动硬件平台。

weixin_29240711的博客 332

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

转载请注明出处:http://blog.csdn.net/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

单细胞拟时序分析实战:用monocle2从Seurat对象构建细胞轨迹(附完整代码)

本文提供了一套完整的单细胞拟时序分析实战教程,详细讲解了如何将Seurat对象转换为Monocle2的CellDataSet,并构建细胞命运轨迹。内容涵盖数据转换、轨迹定义基因选择、降维参数调优、可视化技巧及结果生物学验证,旨在帮助研究者解决实际分析中的常见问题,从静态细胞图谱中挖掘动态发育过程。

weixin_29251337的博客 342

【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的博客 249

信捷 XDH Ethercat 正负极限配置详解与实战技巧

本文详细解析了信捷XDH系列PLC通过EtherCAT总线配置伺服电机正负硬极限的完整流程与实战技巧。内容涵盖硬件开关选型安装、EtherCAT IO信号映射、软件参数设置(包括关键的极性配置)、调试验证方法,以及硬极限与软极限的协同策略。重点阐述了如何避免常见配置错误,确保运动控制系统的最后一道物理安全防线可靠有效,为工程师提供从理论到实践的全面指导。

wdx012345的博客 601

十五、SAP-物料需求计划(MRP)

业务逻辑、功能讲解、图文解析、章节总结

m0_72403327的博客 1091

Leetcode 11. Container With Most Water

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

Za七杂⑧ 204

Flow Matching03-01:微分方程【微分方程(描述变化规律的方程):包含未知函数和它的导数的方程】【公式解(精确)、数值解(逼近)】

好的,我将从数学角度详细解释Flow Matching,从高中生能理解的基础知识开始,逐步深入到高级概念。

u013250861的博客 218

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

【WRF-FDDA】观测 Obs nudging 的配置与使用说明(中)

WRF-FDDA

WW、forever的博客 280

微信小程序 - 最新详细实现“全局实时监听页面发生跳转“ 当页面跳转后立即捕捉并触发某个操作或调用处理函数,小程序整个项目无时无刻只要路由发生变化改变了就能监听,时刻检测页面URL变化(获取路由数据)

微信小程序,vue2,vue3,监听路由,如何监听路由发生变化,如何watch监听路由,怎么全局监听页面发生变化,实现当页面路由发生变化时执行一个函数,当路由变化时执行一些操作执行一段代码,怎么监听路由变化时触发一个函数,监听页面变化,页面间的跳转如何监听,页面跳转时怎么拿到路由及参数,页面路由变化时拿到参数和url,使用watch监听路由变化,页面跳转后怎么触发事件,怎么监听页面跳转,监听页面跳转并拿到路由url路径,小程序怎么全局定义一个专门监听路由的函数,小程序怎么无时无刻实时监听路由页面跳转,监听当

🏆 前端领域优质创作者 4474

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...

我的博客 213

GPU 运维实战:H100/H800/B300 换卡与压测

(烧多久、看什么、什么算合格)。文末还给了一个能直接跑的验收判定脚本,三个真实场景的裁决输出都在。

老韩的Linux云计算架构师进阶之路 270

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

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

假人有限元模型

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

[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

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

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

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

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 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

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

leetcode11. Container With Most Water两根柱子围成最大面积

牛客网第一课第三题 问题:给定一个非负数的数组,数组中的每个值代表一个柱子的高度,柱子的宽度是1。两个柱子之间可以围成一个面积,规定:面积=两根柱子的最小值*两根柱子之间的距离。 例子:数 组[3,4,2,5]。3和4之间围成的面积为0,因为两个柱子是相邻的,中间没有距离。3和 2之间围成的面积为2,因为两个柱子的距离为1,且2是最短的柱子,所以面积=1*2。3和5之间围成的面积为6,因为两个

穷理何须格物的博客 983

leetcode 11. Container With Most Water

因为一些事情已经有两天没刷leetcode了,从今天开始加油!一切都会慢慢变好的。 今日份leetcode 11. Container With Most Water Description: Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n...

weixin_39979073的博客 438

[leetcode] 11. Container With Most Water @ python

原题 https://leetcode.com/problems/container-with-most-water/ 解法 参考: 花花酱 LeetCode 11. Container With Most Water 双指针法, 水桶的面积用min(height[l], height[r])*(r-l)求得, 难点在于如何移动两个指针, 由于两个指针往中间移的时候, 宽度会降低, 我们需要找到更...

闲庭信步 336

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

LeetCode 11. Container With Most Water 题解

本题是双指针的经典应用问题,主要考察对双指针技巧的理解和使用。通过使用两个指针分别指向数组的开头和结尾,我们可以有效地计算出最大的容器面积。双指针的核心思想是:通过移动高度较小的那个指针,我们可以尝试找到更大的高度,从而可能增加容器的面积。这种方法的时间复杂度是 O(n),比暴力解法的 O(n²) 要高效得多。这种方法不仅适用于盛最多水的容器问题,还可以应用于许多其他需要双指针技巧的问题,例如两数之和、三数之和等。掌握双指针的使用,对于解决这类问题非常重要。

cannonjinx的博客 4310

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 e

你若嘻哈过盛,别人就不再欣赏你的认真 705

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...

ccystewart的博客 151
上一篇: 堆和堆排序
下一篇: 链表--穿针引线
Peggy_Chang
博客等级 码龄11年 21粉丝 44原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值