zoukankan      html  css  js  c++  java
  • 算法(5)

    力扣算法四

    求最多的水容器

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
    
    说明:你不能倾斜容器,且 n 的值至少为 2。
    
    
    
    图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。
    
     
    
    示例:
    
    输入: [1,8,6,2,5,4,8,3,7]
    输出: 49
    
    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/container-with-most-water
    
    
    # 双指针 水池容量取决于短的水池边缘,所以从两边开始,如果更短的一边就向中间靠拢,一直到相遇
    height = []
    res,l,r = 0,0,len(height)-1
    while l<r:
        res,l,r = max(res,height[l]*(r-l),(l+1),r) if height[l]<height[r] else max(res,height[r]*(r-l),l,r+1)
    
  • 相关阅读:
    es6 类
    set/ weakset/ map/ weakmap
    async/await
    生成函数
    数组的操作
    解决异步(重点promise函数)
    迭代器
    遍历
    symbol 数据类型
    es6.代理 proxy
  • 原文地址:https://www.cnblogs.com/albert0823/p/11000565.html
Copyright © 2011-2022 走看看