Java实现Container With Most Water

ZLMediaKit在Windows下启用WebRtc编译(64位) 准备软件 cmake-gui vs2017 已经编译好的windows版本openssl(下载地址) 当然,你完全可以自己按照openssl的官网教程自己编译windows版本的openssl libsrtp编译安装 首先,需要将下载好的openssl解压并添加到环境变量。如下图,将下图路径添加到环境变量 其次,下载libsrtp(下载地址) 下载后解压并新建build文件夹,如下图: 在合适位置新建文件夹放置install后的srtp文件,如"D:\develop20.. 阅读详情

算法描述    

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.

        注意: You may not slant the container and n is at least 2.

算法一实现:

public class Solution {
    /**
     * Given n non-negative integers a1, a2, ..., an, where each represents<br>
     * a point at coordinate (i, ai). n vertical lines are drawn such that<br>
     * the two endpoints of line i is at (i, ai) and (i, 0). Find two lines,<br> 
     * which together with x-axis forms a container, such that the container <br>
     * contains the most water.<br>
     * <font color="red">Note: You may not slant the container and n is at least 2.</font>
     * @param  height int[]
     * @return maxArea int
     */
    public int maxArea(int[] height) {
        //height不能为空
        if (height == null) {
            throw new NullPointerException("height数组不能为空");
        }
        //数组元素为两个
        if (height.length == 2) {
            if (height[0] == 0 || height[1] == 0)   return 0;
            else if (height[0] < height[1])         return height[0];
            else if (height[0] >= height[1])        return height[1];
        }
        //数组元素超过两个
        if (height.length > 2) {
            int maxArea  = 0;
            for (int i = 0; i < height.length; i++) {
                if (height[i] == 0) {
                    continue;
                }
                int area = 0;
                for (int j = i + 1; j < height.length; j++) {
                    if (height[j] == 0) {
                        continue;
                    }
                    int areaTemp = 0;
                    if (height[i] < height[j]) {
                        areaTemp = height[i] * (j - i);
                    } else {
                        areaTemp = height[j] * (j - i);
                    }
                    if (area < areaTemp) {
                        area = areaTemp;
                    }
                }
                if (maxArea < area) {
                    maxArea = area;
                }
            }
            return maxArea;
        }
        //数组元素个数小于2时,值为0
        return 0;
    }
}

缺点:算法时间复杂度高,效率低。

算法二实现(优化)

public class Solution {
    public int maxArea(int[] height) {
        //height不能为空
        if (height == null) {
            throw new NullPointerException("height数组不能为空");
        }
        
        //数组个数不小于2时
        if (height.length >= 2) {
            int maxArea = 0;
            int i = 0;
            int j = height.length - 1;
            while (i != j) {
                int area = 0;
                if (height[i] < height[j]) {
                    area = height[i] * (j - i);
                    i++;
                } else {
                    area = height[j] * (j - i);
                    j--;
                }
                if (maxArea < area) {
                    maxArea = area;
                }
            }
            return maxArea;
        }
        //数组元素个数小于2时,值为0
        return 0;
    }
}

变成for循环

public int maxArea(int[] height) {
        //height不能为空
        if (height == null) {
            throw new NullPointerException("height数组不能为空");
        }
        
        //数组个数不小于2时
        if (height.length >= 2) {
            int maxArea = 0;
            for (int i = 0, j = height.length - 1, area = 0; i != j; ) {
                if (height[i] < height[j]) {
                    area = height[i] * (j - i);
                    i++;
                } else {
                    area = height[j] * (j - i);
                    j--;
                }
                if (maxArea < area) {
                    maxArea = area;
                }
            }
            return maxArea;
        }
        //数组元素个数小于2时,值为0
        return 0;
    }
算法二证明:

      在a1,a2.....an中,存在两个点使所求面积最大,所以假设ax,ay为其中使面积最大的两个点,i < j。我们需要证明的是一定能遍历到这两个点即可。i从左到右移动,j从右往左。i和j对应的值哪个小哪个移动,这样始终在找大值,越移动j - i越来越小,高度越来越大,所以一定能找到ax和ay。

再具体证明一下,如下图所示:


图1

        我们需要证明两点:一.要想使ax和ay所在面积最大,a(y+1)的值一定是1或2的位置(即:比ax小的位置);二:如何保证a(y+1)移动到a(y)。

i当前在ax的位置,j当前在a(y+1)的位置。当j向左移动后,j-i变小了,如果想使ax和ay所在面积最大,只能是a(y+1)的值在1和2的位置;要使j向左移动,则只要height[i] > height[j]。所以一定可以移动到ay的位置,证明完成。

01 - 什么是SVM 摘要:SVM(共享虚拟内存)技术使CPU和GPU能共享同一虚拟地址空间,简化异构计算编程。传统模型需手动管理CPU/GPU内存和拷贝数据,而SVM允许直接传递指针,自动处理页面迁移和一致性维护。其核心包括统一地址空间、按需迁移、页面故障处理和一致性管理,显著降低编程复杂度并提升性能。通过HMM框架、MMU通知和GPU页面异常等机制实现高效内存共享,支持复杂数据结构,为异构计算带来便利。 阅读详情

相关推荐

[USACO2023-JAN-Bronze] T2 Air Cownditioning II 题解

[USACO2023-JAN-Bronze] T2 Air Cownditioning II题解

1652

11. Container With Most Water

11. Container With Most Water 方法一:暴力破解 public static int maxArea(Integer[] height) { int mostWater=0; for(int i=0;i<height.length-1;i++){ for(int j=i+1;j<height.length;j++){ int tempWater=(j-i)*Math.min(height[i],height[j]);

noobzzz的博客 155

基于ASP.NET+SQLServer的美妆网站的设计与实现

通过本次ASP.NET MVC美妆商城项目的开发,我们实现了一个功能完善、性能稳定的在线购物平台。该项目不仅提升了用户的购物体验,也为商家提供了一个高效的商品管理和销售渠道。未来,我们将持续优化系统功能,提升用户体验,以适应不断变化的市场需求。

毕业设计大牛哥 1499

Container with most water(盛水最多的容器)

Container with most water(盛水最多的容器) 如图 给定 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。 大致意思为从中选出两个 ...

qq_39686949的博客 771

华硕天选3 win11+ubuntu20.04双硬盘双系统配置(包括ubuntu重启后无法成功进入、没有wifi图标的设置)

1、制作系统盘 2、分区 3、重启电脑按F2

baidu_38431960的博客 1万+

纽扣电池供电系统优化:NBM5100A与STM32F745VG组合方案

在低功耗物联网设备设计中,能量管理是核心挑战之一。纽扣电池因其体积小、成本低被广泛采用,但瞬时大电流需求常导致电压骤降和寿命缩短。通过超级电容缓冲技术,可以实现能量的细水长流式存储与集中释放。NBM5100A电源管理IC与STM32F745VG微控制器的组合方案,采用双阶段能量管理架构,先以恒定小电流充电,再通过电容释放大电流。这种方案不仅能提升系统稳定性,还能延长电池寿命3-5倍,特别适合智能门锁、无线传感器等需要间歇性高功率输出的场景。

weixin_29194255的博客 257

Leetcode 11 container-with-most-water(Java实现)

题目描述 给定n个非负整数a1,a2,…,an,其中每个数字表示坐标(i, ai)处的一个点。以(i,ai)和(i,0)(i=1,2,3...n)为端点画出n条直线。你可以从中选择两条线与x轴一起构成一个容器,最大的容器能装多少水? 注意:你不能倾斜容器 例如: 输入 [1,8,6,2,5,4,8,3,7] 输出: 49 示例1 输入 [1,8,6,2,5,4,8,3,7] 输出 49 思路:容器的高度是两个ai中较小的那个,简单遍历找最大值即可,max表示最大的容器容量。m.

草木如织,匪我思且 329

QuickFix Java 实战(一)协议解析、环境搭建与首个应用

本文是QuickFix Java实战系列的第一篇,详细介绍了FIX协议的核心原理与QuickFix/J引擎的实战应用。内容涵盖FIX协议的标签化通信机制、QuickFix/J库的两种下载方法、协议定义文件解析,并通过一个完整的模拟订单发送示例,指导读者快速搭建首个FIX应用环境,实现消息生成与会话通信。

dog123的博客 495

【LeetCode】No.11. Container With Most Water -- Java Version

【LeetCode】No.11. Container With Most Water -- Java Version

汤姆lazy的优快云博客 506

Reeds-Shepp曲线公式推导及代码实现-第二部分(共三部分)

文章目录2. Reeds-Shepp求解2.1 L+S+L+L^+S^+L^+L+S+L+2.2 L+S+R+L^+S^+R^+L+S+R+2.3 L+R−L+L^+R^-L^+L+R−L+2.4 L+R−L−L^+R^-L^-L+R−L−2.5 L+R+L−L^+R^+L^-L+R+L−2.6 L+Rβ+Lβ−R−L^+R_{\beta}^+L_{\beta}^-R^-L+Rβ+​Lβ−​R−2.7 L+Rβ−Lβ−R+L^+R_{\beta}^-L_{\beta}^-R^+L+Rβ−​Lβ−​R+2.8

jianmo的博客 1212

2026年迈瑞医疗嵌入式岗位面试题集(含参考答案)

文章摘要 本文总结了嵌入式开发面试中的30道题目,涵盖7大模块:嵌入式硬件与底层驱动、C/C++语言基础、操作系统与多线程编程、网络通信协议、数据结构与算法、开发工具与调试能力、综合素养与职业认知。重点解析了I²C通信协议的工作原理、速率选择及异常排查方法;USB的报文协议与四种传输类型及其适用场景;硬中断与软中断的区别及设计注意事项;字节序的概念与处理方法;以及C++中static和const关键字的核心作用。内容深入实用,适用于嵌入式开发者的技术提升与面试准备。

Open 53

11. Container With Most Water盛水最多的容器(C++ 和 Java解法)

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 lines, which together with x-axis forms a container, s...

m0_37536878的博客 338

使用LSTM实现自动调制识别(RML2018.01a)

目录一、LSTM的代码实现1.1 导入包1.2 数据读取及数据集划分1.3 数据预处理1.4 构建LSTM模型 本文基于RML2018.01a大型数据集,给出LSTM的信号识别的实现方式(附代码) RML2018.01a数据集信息如下: 模型 传送门 CNN/ResNet 使用CNN/ResNet实现自动调制识别(RML2018.01a) LSTM 使用LSTM实现自动调制识别(RML2018.01a)) 一、LSTM的代码实现 1.1 导入包 import numpy as np

QAQIknow的博客 6123

Container With Most Water (Java实现)

当看见这道题时想到的一个答案就是暴力破解,附上代码package com.alibaba; import java.util.Scanner; public class Solution { public int maxArea(int[] height) { int maxCapacity = 0; for(int i = 0; i < height.length - 1; i+

Jack小站 359

假人有限元模型

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

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

java 实现Leetcode Container With Most Water

java 实现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 t

嘴巴的专栏 397

Container-with-the-most-water Leetcode(java实现)

Container-with-the-most-water Leetcode(java实现) [题目描述] (https://leetcode.com/problems/container-with-most-water/) 解题方法 设置height数组首末两端同时向中间位置搜索,取左右最小值,并计算面积。 当i&lt;j,并且h等于i对应值时,左侧++; 当i&lt;j,并且h等于j对应值时,...

qq_441467816的博客 162

【LeetCode】11. Container With Most Water - Java实现

文章目录1. 题目描述:2. 思路分析:3. Java代码: 1. 题目描述: Given n non-negative integers a[1], a[2], …, a[n] , where each represents a point at coordinate (i, a[i]). n vertical lines are drawn such that the two endpoint...

小怪孩的成长之路 291

[leetcode]11. Container With Most Water(Java实现)

leetcode:https://leetcode.com/problems/container-with-most-water/#/solutions Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lin

zjkC050818的博客 508

【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

[LeetCode][11]Container With Most Water解析 时间复杂度为O(n) -Java实现

Q: 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,

一些奇奇怪怪的解法 2331

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 佛系刷题 每天动动脑

Leetcode 刷题7. Reserver Integer Leetcode 佛系刷题 Java代码 7. Reserver Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -3...

maowenbei的博客 220

LeetCode 1,11,26

换阵地了,不搞uva直接来LeetCode了,一天两道easy一道medium,一周一道hard,希望可以坚持吧。 Two Sum Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input

AerisIceBear的博客 299

LeetCode 之水箱问题

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

coder 进阶的专栏 5719

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

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

水果君の日常 2386

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

a83610312的专栏 1万+

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

fffupeng的博客 298
上一篇: Java实现Median of Two Sorted Arrays
下一篇: 实现局部放大图片功能
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值