【LeetCode】 11. Container With Most Water 【Python || C++】双语言实现

【逆强化学习-1】学徒学习(Apprenticeship Learning) 逆强化学习算法1:学徒学习。算法原理+算法步骤+代码+仿真:一个基于gym的小车上山的仿真。附赠学徒学习+Q-Table及学徒学习+DQN的全套代码。 阅读详情

题目(来自leetcode网站):

这个题目的意思是,给你排好一堆柱子,找出容量最大的两根;

最基本的想法是用两个for循环,但是这样太慢了;一开始没想出来,后来看了别人的想法。。。

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.

 

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 can contain is 49.

 

Example:

Input: [1,8,6,2,5,4,8,3,7]
Output: 49
#Python版本
class Solution:
    def maxArea(self, height):
        """
        :type height: List[int]
        :rtype: int
        """
        container=0
        begin_h,end_h=0,len(height)-1
        while begin_h<end_h:
            container = (end_h-begin_h)*min(height[end_h],height[begin_h]) if (end_h-begin_h)*min(height[end_h],height[begin_h])>container else container
            if height[begin_h]>height[end_h]:
                end_h-=1
            else:
                begin_h+=1

                    
                
        return container
#C++版本
class Solution {
public:
    int maxArea(vector<int>& height) {
        int max_container=0;
        int begin=0,end=height.size()-1;
        if(height.size()<2){
            return max_container;
        }
        while(begin < end){
            max_container = (end-begin)*get_Min(height[begin],height[end])>max_container ?  (end-begin)*get_Min(height[begin],height[end]) : max_container;
            height[begin]>height[end]? end--:begin++;
        }
        return max_container;
    }
    int get_Min(int x,int y){
        return x<y? x:y;
    }
};

 

6、Cocos2D游戏开发:精灵添加与角色实现 本文详细介绍了在Cocos2D游戏开发中如何添加背景精灵和玩家角色精灵。通过创建GameScene场景、组织项目资源、使用多分辨率图像适配不同设备,并深入讲解了将角色拆分为上下半身以实现更真实瞄准效果的原理。文章还解析了Cocos2D的图像搜索机制、锚点概念及其应用,以及如何通过父子节点关系管理复合角色。最终实现了猎人角色在场景中的正确显示与定位,为后续动画和交互功能打下基础。 阅读详情

相关推荐

SpotBugs使用

spotbugs命令行参数解析

javajingling的专栏 2946

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

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

水果君の日常 2386

Win10 Rational Rose 安装教程

在此教程中我们需要四个文件,点此下载[提取码:ig33] 其中第一个为2007的安装包,第二个为破解文件 前言 很多小伙伴反应说会出现几天后就到期的问题,包括我也出现了该问题,如果你已在其他地方安装好了文件且破解成功,且但几天后到期,如果可以使用更改时间的方式打开,那么可以跳过接下来的教程,直接看第三节,我们采用批处理的形式一键完成从修改时间到打开最后改回来的方法解决此问题 一、安装软件 解压缩两个安装包,进入 Rational Rose文件夹 选择setup.exe开始安装 按照图示依次点击

CoderJiang 2万+

Leetcode11. Container With Most Water

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

magic_jiayu的博客 259

LM Studio 模型加载与转换实战指南:从GGUF格式到本地部署

本文提供了使用LM Studio加载与转换GGUF格式大语言模型的完整实战指南。详细阐述了为何GGUF格式是本地部署的最佳选择,并逐步指导用户如何将非GGUF模型(如Transformers格式)转换为GGUF格式,以及在LM Studio中进行加载、配置GPU卸载等关键优化,最终实现流畅的本地离线对话体验。

weixin_29061425的博客 916

大众奥迪诊断不求人:手把手解析SAE J2819(CAN TP2.0)协议报文与连接流程

本文深入解析大众奥迪车型诊断核心标准SAE J2819(CAN TP2.0)协议,详细拆解连接建立流程与报文控制字段。通过实战案例展示通道设置、参数协商等关键步骤,提供诊断设备配置建议和常见故障排查技巧,帮助工程师掌握汽车诊断通信协议的精髓。

weixin_30421809的博客 396

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

ZkvIA的博客 5857

白炽灯寿命工程解析:从钨丝蒸发到电压控制的可靠性设计

白炽灯作为一种经典的电光源,其工作原理基于电流通过钨丝产生热辐射发光。在高温下,钨丝会持续蒸发导致灯丝逐渐变细、电阻增大,这是影响其寿命的核心物理机制。从工程角度看,白炽灯的可靠性设计涉及材料科学、热力学和电路设计的交叉,技术价值在于通过理解其失效原理,可以在成本与性能间取得最佳平衡。在实际应用中,电压波动、开关浪涌电流和机械振动是主要寿命杀手,尤其在工业控制、仪器仪表和特定照明场景中,合理的设计能大幅提升系统稳定性。本文深入探讨了钨蒸发、热应力等关键因素,并结合浪涌抑制和降压设计等实战技巧,为工程师提供了

weixin_28684655的博客 568

【力扣Leetcode题解系列之0011—Container With Most Water 盛最多水的容器:多语言实现与拓展】

本文介绍了LeetCode中"盛最多水的容器"和"整数转罗马数字"两道题目的解法与拓展。 对于"盛最多水的容器"问题,重点分析了双指针法的解题思路,该方法通过左右指针向中间移动的策略,将时间复杂度优化至O(n)。文章提供了Python、Java、C、C++和Go五种语言的实现代码,并进行了算法复杂度分析。此外还提出了三维容器、可变挡板高度等扩展问题,以及资源分配、图像处理等实际应用场景。

youngerwang的博客 732

8155源码编译DSP(ubuntu环境下)

一.设置环境变量 -users:/$ sudo gedit ~/.bashrc #set adsp cdsp tools path #export ARMLMD_LICENSE_FILE=<LICENSE FILE INFO> #export ARMTOOLS=RVCT221 #export ARMROOT=/pkg/asw/compilers/arm/ADS1.2 #export ARM_COMPILER_PATH=/pkg/qct/software/arm/RVDS/2.2BLD593

Johnny2004的专栏 1676

基于STM32、HAL库的BMP585 气压传感器 驱动程序设计

define BMP585_I2C_ADDR_PRIMARY 0x76 << 1 /* 0x76地址左移一位 */#define BMP585_I2C_ADDR_SECONDARY 0x77 << 1 /* 0x77地址左移一位 *//* I2C句柄 *//* I2C地址 *//* 设置新的温度过采样率 *//* 设置新的滤波器系数 *//* 芯片ID *//* 清除温度过采样位 *//* 转换为hPa *//* 设置新的压力过采样率 *//* 设置新的电源模式 *//* 清除压力过采样位 */

corlin6688的博客 458

[ LeetCode ] #11. Container With Most Water(最大容器 C++ & Python)

题目: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 ii...

xn's blog 245

3、硬件工程师笔试面试学习汇总——器件篇目录(2025版本)

硬件工程师笔试面试学习

XU157303764的博客 7619

LeetCode算法题11:盛最多水的容器(Container With Most Water)

技术交流可以加: 本人微信:xcg852390212 本人qq:852390212 学习交流qq群1(已满): 962535112 学习交流qq群2: 780902027 盛最多水的容器 LeetCode中文 LeetCode英文 给定 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 ...

Making-It 730

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

LeetCode 11 container-with-most-water(双指针)

题目链接:https://leetcode.com/problems/container-with-most-water/ 题意:选择两个垂直线,组成容器,求最大容积 思路:维护左右两个指针,初始为两端,每次选择小的一端往另一端更新(因为容积取决于较小端的高度,所以当前是较小端的最大容积) 代码: class Solution { public: int maxArea(...

我好菜啊 438

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

【LeetCode】11. Container With Most Water 盛最多水的容器

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

负雪明烛 4104

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

LeetCode探索之旅2(92)-11Container with most water

今天继续刷LeetCode,第11题,求最大的储水量。 分析: 方法一:简单粗暴,通过两层循环,找到两根柱子之间的最小值,然后求面积,并输出面积的最大值; 方法二:双指针法,减少比较次数,通过一次遍历找到面积的最大值。 问题: 附上C++代码1: class Solution { public: int maxArea(vector<int>& height) { ...

JerryZengZ的博客 213

LeetCode:11. Container With Most Water - Python

问题描述:11. 盛最多水的容器给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。注意:你不能倾斜容器,n 至少是2。问题分析:给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (...

Growth Diary 1647

leetcode第11题--Container With Most Water

Problem: Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis at (i,ai) and (i...

96

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

dldldl1994的博客 152

LeetCode 11 Container With Most Water (C,C++,Java,Python)

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

算法之道 1930

LeetCode - 11. Container With Most Water

Two Pointers O(n) 面积 = 宽(min of two heights) X 长(the distance between two heights) 1.保证长或宽从大到小:宽比较难从大到小排因为heights都是乱序的;长倒是可以,从两侧的heights开始考虑 2.在保证长是从大到小的情况下,只需要保证宽尽量高就行了:不断替换较矮的那个height Python cla...

real_lisa的博客 165

leetcode 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 of line i is at (i, ai) an...

weixin_30802273的博客 77

leetcode11- Container With Most Water

1,C++解法: class Solution { public: int maxArea(vector&lt;int&gt;&amp; height) { int i=0,j=height.size()-1,m=0; while(i&lt;j) { m=max(m,min(height[i],height[j])*(j-...

年少时那天路上遇见的那位女生的名字,我至今还不知道呢。 208

LeetCode11. Container With Most Water(C++/Python)

Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis at (i,ai) and (i, 0). Find two...

qq_41562704的博客 201

AltTab macOS指南:四步完成类Windows窗口切换设置

AltTab 是一款为 macOS 打造 Windows 式 Alt+Tab 体验的窗口切换工具:按住快捷键弹出窗口预览面板,松开即切换到目标窗口。它覆盖日常多任务场景,并支持按应用、Spaces 桌面与屏幕过滤列表。 [![AltTab macOS 窗口切换预览效果](https://raw.gitcode.com/gh_mirrors/al/alt-tab-macos/raw/2d4f4c8

gitblog_00953的博客 145
上一篇: 【LeetCode】Palindrome Number 【Python || C++】 双语言实现
下一篇: 【LeetCode】String to Integer (atoi) 【Python || C++】
Jie Ou
博客等级 码龄10年 176粉丝 111原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值