[python 刷题] 11 Container With Most Water

这篇具有很好参考价值的文章主要介绍了[python 刷题] 11 Container With Most Water。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

[python 刷题] 11 Container With Most Water

题目:

You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).

Find two lines that together with the x-axis form a container, such that the container contains the most water.

Return the maximum amount of water a container can store.

Notice that you may not slant the container.

这道题也是一个比较经典的双指针+贪心,求的事总面积,题目中已经提出容器无法被倾倒 🫗,在不考虑三角的情况下,面积的构成就是 长(x 轴) x 宽(y 轴) 的组合,

以案例 1 而言:[1,8,6,2,5,4,8,3,7],分析如下:

当左右指针指向最两边时,面积是这样的:

[python 刷题] 11 Container With Most Water,# leetcode,python,开发语言

此时面积为 m i n ( 1 , 7 ) × 8 min(1, 7) \times 8 min(1,7)×8

可以看到,容器的面积是收到长和高的限制,而高的选取则为 m i n ( l e f t , r i g h t ) min(left, right) min(left,right),也就是说,高都不变,指针之间的距离越小,面积自然就越小。

这个情况下,最合理的做法就是移动较低一边的指针,这样长度虽然减少了,但是高度增加的话,面积还是有可能会增加:

[python 刷题] 11 Container With Most Water,# leetcode,python,开发语言

此时的面积为 m i n ( 7 , 8 ) × 7 min(7, 8) \times 7 min(7,8)×7

接着重复这样的操作:

[python 刷题] 11 Container With Most Water,# leetcode,python,开发语言

一直到完成遍历即可

代码如下:

class Solution:
    def maxArea(self, height: List[int]) -> int:
        res = 0
        l, r = 0, len(height) - 1

        while l < r:
            res = max(res, min(height[l], height[r]) * (r - l))
            if height[l] < height[r]:
                l += 1
            else:
                r -= 1

        return res

这道题的空间复杂度为 O ( 1 ) O(1) O(1),只需保留最终结果和 2 个指针,时间复杂度为 O ( n ) O(n) O(n)

多提一句,只是针对这道题,greedy 一定能够导出有最优解,所以没有必要使用 DP,DP 因为要寻找全局最优解,所以时间复杂度为 O ( n 2 ) O(n^2) O(n2)文章来源地址https://www.toymoban.com/news/detail-732229.html

到了这里,关于[python 刷题] 11 Container With Most Water的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • python LeetCode 刷题记录 28

    给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串的第一个匹配项的下标(下标从 0 开始)。如果 needle 不是 haystack 的一部分,则返回 -1 。

    2024年02月08日
    浏览(12)
  • 力扣python刷题day03|LeetCode203、707、206

    力扣python刷题day03|LeetCode203、707、206

    题目 题目链接:203:移除链表元素 方法一: 知识点: 设置虚拟头结点 题目 来源:力扣(LeetCode) 提示: 0 = index, val = 1000 请不要使用内置的 LinkedList 库。 调用 get、addAtHead、addAtTail、addAtIndex 和 deleteAtIndex 的次数不超过 2000 。 方法一:单链表法 方法二:双链表法 (有点难

    2024年02月16日
    浏览(30)
  • 力扣python刷题day04|LeetCode24、19、160、142

    力扣python刷题day04|LeetCode24、19、160、142

    题目 来源:24:两两交换链表中的节点 建议使用虚拟头结点,这样会方便很多,要不然每次针对头结点(没有前一个指针指向头结点),还要单独处理。 接下来就是交换相邻两个元素了,此时一定要画图,不画图,操作多个指针很容易乱,而且要操作的先后顺序 方法: 题目

    2024年02月16日
    浏览(12)
  • 【Leetcode刷题-Python/C++】长度最小的子数组(滑动窗口)

    209.长度最小的子数组 给定一个含有 n 个正整数的数组和一个正整数 target 。 找出该数组中满足其和 ≥ target 的长度最小的 连续子数组 [numsl, numsl+1, …, numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。 输入:target = 7, nums = [2,3,1,2,4,3] 输出:2 解释:子数

    2023年04月08日
    浏览(6)
  • Leetcode 508. Most Frequent Subtree Sum (二叉树后序遍历好题)

    Most Frequent Subtree Sum Solved Medium Topics Companies Given the root of a binary tree, return the most frequent subtree sum. If there is a tie, return all the values with the highest frequency in any order. The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself). Example

    2024年02月22日
    浏览(11)
  • VS Code(Visual Studio Code)本地(local)和远程(ssh)Docker Container 下的 Python 开发和调试

    VS Code(Visual Studio Code)本地(local)和远程(ssh)Docker Container 下的 Python 开发和调试

    我们通常在 Python 上进行 人工智能算法 开发,但是这通常需要 专用的运行环境、依赖库和配置文件 。为了 人工智能算法 开发的便利,通常会使用 Docker,因为 Docker 可以将我们的人工智能算法工程打包封装到一个 Container (容器)中,该 Container (容器)包含了 人工智能算法

    2024年03月20日
    浏览(28)
  • 突发情况2-Python 3.11.0 安装pygame提示error: subprocess-exited-with-error

    突发情况2-Python 3.11.0 安装pygame提示error: subprocess-exited-with-error

    1.pip3 install pygame 后 报错提示: 2.翻了各种文章后理解可能为版本不兼容导致 pygame公测版无法在高python版本下安装 于是使用 pygame的体验版即可 pip3 install pygame --pre 3.参考文献 :https://stackoverflow.com/questions/64311396/pygame-no-setup-file-exists-running-buildconfig-config-py 中评论: 9 I had the

    2024年02月02日
    浏览(9)
  • docker 启动容器异常Error response from daemon: OCI runtime create failed: container with id exists

    docker 启动容器异常Error response from daemon: OCI runtime create failed: container with id exists

    问题描述 docker服务异常停止,重启docker后,容器启动失败 错误信息 Error response from daemon: OCI runtime create failed: container with id exists: xxx unknown 错误原因 docker启动的时候,会在运行目录(/var/run/docker/runtime-runc/moby)(不同环境,可能目录不一样,可以通过 find / -name \\\'容器ID\\\' 查找

    2024年02月16日
    浏览(16)
  • 解决 Application xxx failed 2 times due to AM Container for xxx exited with exitCode: 13 问题

    解决 Application xxx failed 2 times due to AM Container for xxx exited with exitCode: 13 问题

    我安装的是Hadoop3.3.4,使用的是Java17,Spark用的是3.3.2 启动完成后,我在控制台输入如下命令 出现报错信息 进入spark下的sbin目录,输入下面的命令 我的在 /opt/spark/conf 这个路径下 找到这里,把红框的端口号进行修改 与hadoop下的core-site.xml文件中的fs.defaultFS端口号一致 修改完后

    2024年02月03日
    浏览(13)
  • 运行Mapreduce集群时候出现报错:Container exited with a non-zero exit code 1. Error file: prelaunch.err. Last 40

    运行Mapreduce集群时候出现报错:Container exited with a non-zero exit code 1. Error file: prelaunch.err. Last 40

    Container exited with a non-zero exit code 1. Error file: prelaunch.err. Last 4096 bytes of prelaunch.err : Last 4096 bytes of stderr : 错误: 找不到或无法加载主类 org.apache.hadoop.mapreduce.v2.app.MRAppMaster 解决方法: 在主机中运行: 记下返回的结果 添加一个配置: 加入返回的信息: 加入之后如下图: 再次运行

    2024年02月16日
    浏览(15)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包