码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【力扣 - 盛最多水的容器】


    题目描述

    给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。

    找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

    返回容器可以储存的最大水量。

    说明:你不能倾斜容器。

    示例1

    在这里插入图片描述

    示例2

    输入:height = [1,1]
    输出:1

    提示:

    n == height.length
    2 <= n <= 10^5
    0 <= height[i] <= 10^4

    题解

    双指针

    1:使用两个指针,indexLeft和indexRight
    2:面积的计算公式Area = high * wide,面积等于高 * 宽
    3:计算high = fmin(height[indexRight] , height[indexLeft])
    4: 计算wide = indexRight - indexLeft
    5:计算出对应的Area与maxArea进行比较并更新maxArea
    6: 移动indexRight 与 indexLeft中小的指针
    时间复杂度:O(n)
    空间复杂度:O(1)

    代码

    int maxArea(int* height, int heightSize)
    {
        // Initialize two pointers, one at the start and one at the end of the array
        int indexLeft = 0;
        int indexRight = heightSize - 1;
        
        // Initialize the variable to store the maximum area
        int maxArea = 0;
        
        // Loop until the two pointers meet
        while (indexLeft < indexRight)
        {
            // Calculate the height of the container at the current positions
            int minHeight = fmin(height[indexLeft], height[indexRight]);
            
            // Calculate the width of the container
            int width = indexRight - indexLeft;
            
            // Calculate the area of the container
            int area = width * minHeight;
            
            // Update the maximum area if the current area is greater
            if (maxArea < area)
            {
                maxArea = area;
            }
            
            // Move the pointer with the smaller height towards the center
            if (height[indexLeft] < height[indexRight])
            {
                indexLeft++;
            }
            else
            {
                indexRight--;
            }
        }
        
        // Return the maximum area found
        return maxArea;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
  • 相关阅读:
    无swing,高级javaSE毕业之贪吃蛇游戏(含模块构建,多线程监听服务),已录制视频
    网络安全合规-DSMM
    REDIS01_概述、安装、key、字符串String、列表List、集合Set、Hash哈希、Zset有序集合、持久化策略
    基于FPGA的电风扇控制器verilog,视频/代码
    trino安装及使用
    GET 和 POST 方式区别
    美团、飞猪基础架构组实习经历分享
    【刷题篇】贪心算法(二)
    java计算机毕业设计基于ssm的协同过滤算法的电影推荐系统(源代码+数据库+Lw文档)
    B/S架构下的界面控件DevExtreme甘特图有哪些功能?一起来看
  • 原文地址:https://blog.csdn.net/shuting7/article/details/136451502
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号