Leetcode 11. Container With Most Water , Java解法

AI编程实战:用Spec Coding与Codex半小时重构全栈项目 这次我们来看一个能显著提升全栈开发效率的实战方法:利用 AI 编程工具 Codex 和 Spec Coding 工作流,将原本需要数周甚至一个月的全栈项目重构工期,压缩到半小时级别。这不是概念探讨,而是基于现有工具链的、可立即上手的工程实践。 对于前端开发者或全栈工程师而言,面对老旧项目重构、技术栈升级或从零搭建新项目时,最大的痛点往往是重复性、模式化的代码编写工作。手动编写 CRUD 接口、页面组件、状态管理和联调测试,耗时费力且容易出错。而 AI 辅助编程的核心价值,正是在于将开发者从这些“体力活”中解 阅读详情

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

Approach #1 Brute Force 我的解法

public class Solution {
    public int maxArea(int[] height) {
        int max = 0;
        int len = height.length;
        // try every possible pair
        for(int i=0;i<len;i++){
            for(int j=i+1;j<len;j++){
                max = Math.max(max,Math.min(height[i],height[j])*(j-i));
            }
        }
        return max;
    }
}

Complexity Analysis

  • Time complexity : O(n^2). Calculating area for all \dfrac{n(n-1)}{2}​ height pairs.
  • Space complexity : O(1). Constant extra space is used. 

Approach #2: Two Pointer 

public class Solution {
    public int maxArea(int[] height) {
        int maxarea = 0, l = 0, r = height.length - 1;
        while (l < r) {
            maxarea = Math.max(maxarea, Math.min(height[l], height[r]) * (r - l));
            if (height[l] < height[r])
                l++;
            else
                r--;
        }
        return maxarea;
    }
}

从Array的两边分别开始计算乘积(距离,和两端更矮的一端的高度),然后更新最大值,移动更矮一端的坐标。

Complexity Analysis

  • Time complexity : O(n) Single pass.

  • Space complexity : O(1). Constant space is used.

OpenClaw插件配置错误修复完全指南 插件文件名是插件目录结构正确package.json配置正确OpenClaw的插件系统还在快速迭代,偶尔会遇到这种配置问题。看错误信息检查配置文件检查实际文件对比差异修复问题大部分问题都能解决。查看官方文档:https://openclaw.ai/docs提Issue:https://github.com/openclaw/openclaw/issues加入Discord社区求助快速修复命令汇总。 阅读详情

相关推荐

TCP协议中ARQ自动重传的三种实现方式对比(含SACK详解)

本文深入解析TCP协议中ARQ自动重传的三种实现方式,包括停止-等待、回退N步和选择重传协议,特别详细介绍了SACK机制的工作原理和优势。通过对比分析各协议的效率、复杂度和适用场景,为网络通信中的可靠性传输提供了实用的选型指南。

weixin_30776273的博客 462

LeetCode11 Container With Most Water Java解法

题意分析 给定 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) 。画 n 条垂直线,使得垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的矩形可以容纳最多的水。 注意:你不能倾斜容器,n 至少是2   比较容易想到的解法,0(n2) 遍历全部 class Solution { pub...

qq_31059853的博客 357

从逻辑门到存储阵列:NAND与NOR闪存命名与结构原理详解

在数字电路与半导体存储器领域,逻辑门是构建一切复杂功能的基础单元,其输入输出关系定义了计算与存储的基本原理。与非门(NAND)和或非门(NOR)作为通用逻辑门,其真值表和行为模式深刻影响了芯片设计。基于这些原理,现代闪存技术发展出了两种核心架构:NAND Flash和NOR Flash。这两种闪存的命名直接源于其内部存储单元阵列的连接方式,分别模拟了与非门和或非门的逻辑关系。这种结构差异决定了它们根本的技术价值:NOR Flash采用并联结构,支持快速随机读取,适用于代码存储与原地执行(XiP);而NAND

weixin_33377602的博客 339

LeetCode 11. Container With Most Water--Java 解法--困雨水简单版

LeetCode 11. Container With Most Water–Java 解法 此文首发于我的个人博客:LeetCode 11. Container With Most Water–Java 解法–困雨水简单版 — zhang0peter的个人博客 LeetCode题解文章分类:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 题目地址:Con...

个人博客 795

基于Freertos与lwip移植FreeModbusTcp协议

基于FreeRTOS的N32G457VEL7之LWIP网络协议栈移植-优快云博客。

Dd_456456的博客 3239

【数据分享】中国371个城市的坡度矢量数据和excel数据

在城市发展的进程中,地形地貌始终是影响规划决策的关键因素,而坡度作为表征地表倾斜程度的核心指标,更是贯穿于城市建设、生态保护等诸多环节。本文将全面解读中国 371 个城市坡度数据的相关情况,包括数据概况、生成流程、应用场景等内容,为读者呈现一份详尽的坡度数据指南。

qq_39719713的博客 1844

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

机器学习笔记 - 神经辐射场(NeRF)的简要概述

神经辐射场十分重要。在表示和渲染 3D 场景领域,神经辐射场 (NeRF) 在准确性方面取得了巨大突破。给定底层场景的多个图像,NeRF 可以从任意视点重建该场景的高分辨率、2D 渲染图。与局部光场融合 (LLFF) 和场景表示网络 (SRN) 等现有技术相比,NeRF 更能够捕获场景外观和几何形状的复杂组成部分(例如,依赖于视图的反射和复杂的材料)。NeRF的研究目的是合成同一场景不同视角下的图像。

学以致用 知行合一 1693

【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

CAN(2)-GD32

GD32中CAN的特征。

小柚子的博客 553

2025.06.18【转录组】|Ribo-seq/small RNA数据分析:rRNA和smRNA比对率统计全流程

本文介绍了一种自动化统计rRNA/smRNA比对率的方法,用于评估Ribo-seq或small RNA测序数据的质量。主要内容包括:分析原理(比对率定义、参考序列来源)、实践流程(构建索引、比对统计)以及结果可视化方法。通过Python和R脚本实现自动化处理,可生成汇总表格和柱状图。比对率结果能反映样品污染程度,建议控制在20%以下。该流程有助于快速评估测序质量,为后续分析提供可靠依据。文中还提供了常见问题解决方案和脚本示例,适用于生物信息学标准化质控环节。

1616

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

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

水果君の日常 2386

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

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

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

穷理何须格物的博客 983

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

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

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

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

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

LeetCode#11. 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 the line i is at (i, ai) and (i, 0). Find two lines, which, together w

ashtime 188

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

Java详解LeetCode 热题 100(05):LeetCode 11. 盛最多水的容器(Container With Most Water)详解

理解容器容量的计算方式:容量 = 宽度 × 高度,其中高度取决于较短的柱子双指针技巧的应用:从两端开始,逐步向中间移动,每次移动较短的那一端合理剪枝:跳过那些不可能产生更大容量的情况处理边界情况:如只有两根柱子、全部相同高度等优化思路:从暴力法O(n²)到双指针法O(n)的优化过程。

yk_dflkg的博客 1550

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

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

leetCode11题:Container With Most Water

给你 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。 1.直接处理,所谓的暴力解题法: /** * @param {number[]} height * @return {number} */ var maxArea = function(height) { var area = 0; for(

指南针的博客 263

【Java】【LeetCode】11. Container With Most Water

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

zhelong19830726的博客 275

LeetCode 11. Container With Most Water题解

题目地址 Brute force 两层循环,遍历所以情况,找最大结果 public int maxArea(int[] height) { int ans = 0, temp = 0; for(int i = 0; i < height.length; i++) { for(int j = i + 1; j < height.length; j++...

qq_42308861的博客 201

LEETCODE 11 Container With Most Water (JAVA题解)

https://leetcode.com/problems/container-with-most-water/ 原题链接。 题意解析: 给你一个顶点数组,例如{4,7,9},这个定点数组代表直角坐标系上三个点,(1,4),(2,7),(3,9),然后过这三个点,分别作垂直于X轴的线段,例如对于(1,4),线段的两个端点为(1,4)和(1,0),然后,我们可以得到三条垂直于X轴的线段

LCLSpring 858

leetcode11:Container With Most Water

题目:给出一个非负整数数组,数组中的数代表一个栏杆,寻找两个栏杆和x轴形成一个水池,找出能装水最多的两个栏杆。   假如给出的例子是height = {1,2,3,4,5,6,7,8,9},则height[4]=5,height[8]=9,则形成的水池是(9-5)*5=20 思路:先用两个指针,一个low一个hight,一个指向数组开头,一个指向数组结尾,则我们要找的两个栏杆肯定在这两

每天进步一点点 443

11. Container With Most Water

11. Container With Most Water

weixin_45428910的博客 423

LeetCode 11. Container With Most Water

分析 思路 //左侧从左向右,右侧从右向左 //如果左侧比右侧低,则左侧右侧之间的节点与左侧节点之间面积不必计算;//容器高度按左侧计算,宽度不及到右侧的宽度//可直接左侧右移 //如果右侧壁左侧第,则左侧右侧之间的节点与右侧节点之间面积不必计算。//可直接右侧左移 难度 Medium 来源 https://leetcode.com/problems/container-with-most-wat...

ov流岚vo的专栏 266
上一篇: 公开课 CS61B Data Structures, UCB
下一篇: Leetcode 509. Fibonacci Number , Java 解法
YuanTheCoder
博客等级 码龄9年 2粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值