leetcode题解:第11题Container With Most Water

流媒体弱网优化之路(NACK)——纯NACK方案的优化探索 流媒体学习之路(NACK)——纯NACK方案的最高性能 文章目录流媒体学习之路(NACK)——纯NACK方案的最高性能一、NACK简介二、Nack的理解2.1 一种补救策略2.2 什么时候要?2.3 要多少?2.4 怎么要?三、计算分析3.1 rtt强相关3.2 补包次数计算3.3 Nack的定时发送3.4 nack重传限制四、实验   近期在针对纯丢包场景做一些简单的探索实验,一个让我困扰的问就是:不使用FEC而只是通过纯NACK进行补偿,到底能在实验环境下抵抗多少的丢包呢?每一次丢包补偿的最大时长 阅读详情

https://leetcode-cn.com/problems/container-with-most-water/

分析

这道题乍一看与接雨水那道题很像,但那题是求总共能接到的水量,这题则是要求两个边界组成的容器的最大容量。暴力解法是枚举所有的左右边界,计算其容器的容量,再取最大值,时间复杂度为 O ( n 2 ) O(n^2) O(n2),超时了。

解法

这道题要用双指针来解答, 如果给你双指针这个提示的话,你应该会容易想到每次移动更矮的那个边界会是正确的做法,但是怎么证明呢?
对于所有的这些线,它们都有可能作为一个容器的边界。设最左的线高度为 x x x,最右的线高度为 y y y,线之间间隔一共为 t t t,则 x x x y y y组成的容器的容量为: m i n ( x , y ) ∗ t min(x,y)*t min(x,y)t
我们假设 x < = y x <= y x<=y,如果移动 y y y的话,必只能向左移动,新的线高度为 y 1 y_1 y1,容易知道
m i n ( x , y 1 ) < = m i n ( x , y ) min(x,y_1) <= min(x,y) min(x,y1)<=min(x,y)而新的容器容量为 m i n ( x , y 1 ) ∗ ( t − 1 ) < m i n ( x , y ) ∗ t min(x,y_1) * (t-1) < min(x,y) * t min(x,y1)(t1)<min(x,y)t,所以,不论怎么移动右边界,组成的容器容量都是会变小的,换句话说, x x x为边界的最大容量的容器已经找到了 x x x已经没有必要出现在之后的讨论中,所以我们移动左边界,并将 x x x这条线视为消失了。

代码
class Solution {
public:
    int maxArea(vector<int>& height) {
        int result = 0;
        int left = 0, right = height.size() - 1;
        while (left < right) {
            result = max(result, min(height[left], height[right]) * (right - left));
            if (height[left] < height[right]) left++;
            else right--;
        }
        return result;
    }
};
复杂度分析

时间复杂度 O ( n ) O(n) O(n),空间复杂度 O ( 1 ) O(1) O(1)

CAM350常用快捷键 CAM350快捷键 CAM350常用快捷键 阅读详情

相关推荐

18.EC实战 新建项目工程并配置各个引脚的工作方式(持续更新)

本博文介绍了从零开始新建EC工程及对各个引脚进行了工作模式的初始化配置

qq_30135687的博客 1693

Leetcode-11:盛最多水的容器(双指针解法)

目链接 https://leetcode-cn.com/problems/container-with-most-water/ 目 给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点(i,ai) 。在坐标内画 n 条垂直线,垂直线 i的两个端点分别为(i,ai) 和 (i, 0) 。找出其中的两条线,使得它们与x轴共同构成的容器可以容纳最多的水。 说明:你不能倾斜容器。 (该为字节跳动等经典面试) 示例 示例 1: 输入:[1,8,6,2,5,...

道纪书生的博客 181

华为OD机试双机位C卷-MVP争夺战 (C/C++/Py/Java/Js/Go)

华为OD机试(机考)双机位C卷 【MVP争夺战】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做。华为OD机试 - MVP争夺战,

qq_45776114的博客 1149

leetcode-题解-盛最多水的容器(数学 证明 双指针法)

目:https://leetcode-cn.com/problems/container-with-most-water/ 给你 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/container-with

DRD 350

力扣【LeetCode】—— 11. 盛最多水的容器【java】

目地址:https://leetcode-cn.com/problems/container-with-most-water/ 目:  给你 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。 说明:  不能倾斜容器。 示例: 解法一(暴力求解):  思路:遍历计算最大值,两层循环,选中一条垂直线,然后根据这

眉头的骰 879

LeetCode-11盛最多水的容器

文章目录1 目2 暴力法3 双指针法while写法for写法 https://leetcode-cn.com/problems/container-with-most-water/ https://leetcode.com/problems/container-with-most-water/ 1 目 给定 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) 。在坐...

hongxue8888的博客 281

数据结构与算法系列(数组、链表、跳表)

两数之和目: https://leetcode-cn.com/problems/two-sum/ Array 实战目 • https://leetcode-cn.com/problems/container-with-most-water/ • https://leetcode-cn.com/problems/move-zeroes/ • https://leetcode.com/problems/climbing-stairs/ • https://leetcode-cn.com/problems/3

weixin_45996005的博客 262

LeetCode 11. Container With Most Water 题解

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

cannonjinx的博客 2433

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

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

水果君の日常 2381

Leetcode11Container 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的博客 183

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的博客 271

11. Container With Most Water

11. Container With Most Water 题解: 暴力搜索法,时间复杂度 O(n²),空间复杂度 O(1)。 class Solution { public: int maxArea(vector<int>& height) { int maxV = 0; for(int i = 0; i < height...

qq_40491305的博客 247

【Python刷力扣hot100】11. Container With Most Water

摘要 本文介绍了解决"盛最多水的容器"问的两种方法。暴力解法通过双重循环计算所有可能的容器面积,时间复杂度为O(n²),在数据量大时效率低下。更优的双指针法通过初始化左右指针,每次移动较短的一侧来缩小搜索范围,时间复杂度降为O(n)。该方法的关键在于正确性证明:移动短板可能获得更大面积,而移动长板必然导致面积减小。双指针法在保证正确性的同时显著提升了算法效率。

tao355667 659

刷尽天下 -- LeetCode 11 -- Container With Most Water(盛最多水的容器)

链接:https://leetcode-cn.com/problems/container-with-most-water/description/ 看着上面图片就可以知道,容纳水量最多其实就是(j-i)*min(aj,ai),其中j和i为这两条垂直线的横坐标,aj和ai为其纵坐标。 思路:设立两个指针,一个从头一个从尾,相向而行遍历数组,每次舍弃较短边 (1)计算面积最大值的初值...

小岁月太着急 381

Leetcode题解之动态规划(0)盛最多水的容器

目:https://leetcode-cn.com/problems/container-with-most-water/description/ 目描述: 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可...

Esy_Hong 911

LeetCode11. Container With Most Water 盛最多水的容器

LeetCode11. Container With Most Water报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目地址:https://leetcode.com/problems/container-with-most-water/description/ 目描述: Given...

负雪明烛 4077

第3课-数组、链表、跳表

数组、链表(Array、 Linked List) Array • Access: O(1) • Insert: 平均 O(n) • Delete: 平均 O(n) Doubly Linked List 时间复杂度 space O(n) prepend O(1) append O(1) lookup O(n) insert O(1) dele...

hongxue8888的博客 645

leetcode_11 盛最多水的容器

目描述 https://leetcode-cn.com/problems/container-with-most-water/description/ 题解 class Solution: def maxArea(self, height): &quot;&quot;&quot; :type height: List[int] :rtype: int ...

Ding_xiaofei的博客 249

LeetCode 11. Container With Most Water, 盛最多水的容器 ,C#

前言 本文介绍了 LeetCode11 , “Container With Most Water”, 也就是 “盛最多水的容器” 的问. 本文使用 C# 语言完成目。 目 English LeetCode 11. Container With Most Water Given n non-negative integers a1, a2, …, an , where each re...

wf824284257的博客 537

halcon-13.0.1.1-runtime-windows.exe

halcon-13.0.1.1-runtime-windows.exe

STM32F103驱动CCS811

STM32F103驱动CCS811,STM32VET6。实现二氧化碳和TVOC浓度读取

上一篇: leetcode题解:第10题Regular Expression Matching
下一篇: leetcode题解:第15题3 Sum
chenf99
博客等级 码龄10年 44粉丝 83原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值