LeetCode | 11. Container With Most Water

Easy3D&CGAL文章目录汇总 阅读详情

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.

解题思路:注意测试样例会出现高度为0的情况.
解法一:暴力枚举,O(n^2),47/49Passed,果断超时

class Solution {
public:
        int min(int a,int b)
        {
            return a<b?a:b;
        }

        int maxArea(vector<int>& height)
        {
            int len = height.size(), mmax = 0, t_sum = 0;
            for(int i=0;i<len;i++)
            {
                for(int j=i+1;j<len;j++)
                {
                    int tmin = min(height[i],height[j]), tmax = max(height[i],height[j]);
                    if(tmin == 0)
                    {
                        continue;
                    }
                    t_sum = 0;
                    t_sum += (j-i)*tmin;
                    if(t_sum > mmax)
                    {
                        mmax = t_sum;
                    }
                }
            }
            return mmax;
        }
};

解法二:从两端向中间枚举,每次比较面积与最优解的大小,更新最优解.
若height[L]< height[R],L++;否则R–;
证明过程:
假设这个算法找到的解不是最优解.那么存在hl,hr,使得这两个高度组成的矩形面积最大.那么我们算法在执行过程中肯定先遇到了其中的某个,但没有同时遇到.假设算法执行时遇到了hl(并且假设hl高度更大,不然可以换成hr).他肯定还没有遇到hr,不然最优解已经出现并更新.那么此时若hl继续移动,肯定不会同时遇到了,假设此时hl不移动.但是由题意可知,hl不移动的情况只有两种:
1.另一个指针也指向hl,两指针相遇,算法结束.
2.另一指针指向了ht,并且ht的值不必hl小.此时hl要移动.
那么矛盾出现了,既然hl是hl hr中较大者,现在遇到了ht,并且组成的矩形的长和宽都不比hl,hr组成的小,面积不小于它,那么与hl,hr为最优解矛盾.
所以算法的正确性得以证明.

//19ms AC
class Solution {
public:
    int min(int a,int b)
    {
        return a<b?a:b;
    }

    int maxArea(vector<int>& height)
    {
        int len = height.size(), mmax = 0, t_sum = 0;
        int left = 0, right = len-1;
        while(left < right)
        {
            int tmin = min(height[left],height[right]);
            t_sum = (right-left)*tmin;
            if(t_sum > mmax)
            {
                mmax = t_sum;
            }
            if(height[left] < height[right])
            {
                left++;
            }
            else
            {
                right--;
            }
        }
        return mmax;
    }
};
STM32 使用IQmath实现SVPWM 文章目录SVPWMIQMATHIQmathLib测试部分程序附件 SVPWM SVPWM是空间矢量脉宽调制(Space Vector Pulse Width Modulation)的简称,通常由三相逆变器的六个功率开关管组成,经过特定的时序和换相所所产生的脉冲宽度调制波,最终输出的波形可能会十分接近理想的正弦波形。 具体如下图所示;左侧为复平面,即空间矢量,右侧为时域的正弦波形; 关于SVPWM原理的文章非常多,这里可以推荐一下网上一个非常不错的教程《SVPWM的原理及法则推导和控制算法详解第五修改版》, 阅读详情

相关推荐

等保2.0下可信验证的动态实现与合规实践

本文深入解读等保2.0标准中的“可信验证”要求,并重点探讨其动态实现与合规实践。文章阐述了从可信根、信任链到动态验证的核心概念,拆解了不同安全等级的实施差异,并提供了从硬件选型、软件赋能到平台集成的四步落地指南。通过行业案例与避坑心得,为构建覆盖计算环境、区域边界、通信网络和安全管理中心的全链条可信防御体系提供了实用参考。

d3e4f的博客 831

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

告别Vivado自带编辑器!手把手教你用Sublime Text 4 + Icarus Verilog搭建FPGA开发环境(Windows 10/11)

本文详细介绍如何在Windows 10/11系统下,使用Sublime Text 4和Icarus Verilog搭建高效的FPGA开发环境,替代Vivado自带编辑器。通过配置语法检查、代码高亮和智能补全等功能,提升Verilog开发效率,实现专业级工作流。

weixin_30684743的博客 431

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

手把手教学,Python 游戏编程之实现飞机大战(含源代码)

我想大家都是有玩过类似飞机大战的射击类游戏,也享受目标被消除通过后带来的愉悦感。那么如果用Python来实现飞机大战游戏?下面就跟大家一起用Python来做一个小游戏,不仅可以从中感受到乐趣,还可以提升自己的能力。

veratata的博客 1万+

python中frida的安装+frida-server(雷电模拟器)保姆级安装教程

frida的安装,安卓逆向,adb

2301_80224944的博客 4591

Leetcode 11. Container With Most Water

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

Za七杂⑧ 206

手把手教你学AI GPU UMD驱动软件开发专栏--1.1 UMD(用户模式驱动)的定义与定位

摘要:UMD(用户模式驱动)是GPU驱动架构中的关键组件,负责连接AI应用与硬件。作为运行在用户空间的动态链接库,UMD通过减少系统调用开销显著提升AI计算性能。其核心功能包括API转换、资源管理和命令生成,同时具备安全性高(崩溃不影响系统)、开发灵活等优势。随着AI发展,UMD已从图形驱动演变为深度学习加速的关键组件,支持TensorCore等硬件特性优化。未来趋势包括深度框架集成、硬件感知增强和虚拟化支持。UMD通过性能优化和安全隔离机制,成为现代AI计算基础设施的理想选择。

MHD0815的博客 172

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

27、VXLAN BGP EVPN网络的自动化部署与运维管理

本文深入探讨了VXLAN BGP EVPN网络从初始配置到运维管理的全生命周期自动化流程,涵盖Day-0设备自动上线、Day-0.5增量变更与连接验证、Day-1覆盖服务配置与计算集成,以及Day-2监控与故障排查。详细介绍了推送与拉取两种配置模型、主流工具如Cisco VTS、NFM、DCNM的功能特点,以及开源自动化工具的应用。通过流程图和命令示例,系统展示了MAC/IP学习、BGP通告和VXLAN封装的验证方法,为构建高效、可扩展的数据中心网络提供了全面的技术指导。

banana的博客 81

OpenClaw进阶实战(二十五):抖音企业号——私信回复与视频评论分析

本文介绍了抖音企业号自动化运营的技术方案,重点解决私信互动效率低和风控严格两大挑战。文章分析了抖音集成的三大技术壁垒(视频复杂性、风控体系、封闭API)并对比了三种技术路径的优缺点。针对不同规模的运营主体,推荐官方API+RPA混合架构或真机RPA+AI辅助的方案。 详细讲解了环境准备步骤,包括抖音Skill安装、Cookie获取和配置文件设置。核心实现部分展示了私信自动回复功能的安全合规实现,包含意图分类、优先级排序、延迟发送等防封禁机制,并提供了完整的Python代码示例。该方案通过模拟人工操作、限制日

PeterPan的博客 161

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

在Android中实现SIM卡的读写操作

在上述代码中,我们创建了一个PhoneStateListener的实例,并重写了onSimStateChanged方法来处理SIM卡状态改变的事件。然后,我们通过调用telephonyManager的listen方法注册了这个监听器,并指定了要监听的事件类型为SIM卡状态的改变。在上述代码中,我们调用telephonyManager的sendUssdRequest方法来发送USSD命令,并通过TelephonyManager.UssdResponseCallback来处理USSD响应和发送失败的情况。

2301_79326510的博客 928

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

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

[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的博客 146

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

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

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

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

自己的学习笔记 202

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 218

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

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

cannonjinx的博客 4310

[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(容器装水最大量)

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 | 9. Palindrome Number(回文串)
下一篇: LeetCode | 12. Integer to Roman
PKU_CXK
博客等级 码龄10年 87粉丝 142原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值