题目英文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] 题目中文给定 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] 算法实现利用暴力算法 public class Solution { 利用双指针算法 以0-7走到1-7这一步为例,解释为什么放弃0-6这一分支: 用h(i)表示第i条线段的高度,S(ij)表示第i条线段和第j条线段圈起来的面积。 把每一棵子树按照同样的方法分析,很容易可以知道,双指针法走的路径包含了最大面积。 参考图文: https:///problems/container-with-most-water/solution/zhi-guan-de-shuang-zhi-zhen-fa-jie-shi-by-na-kong/ public class Solution { 实验结果利用暴力算法
利用双指针算法
相关图文1. “数组”类算法 2. “链表”类算法 3. “栈”类算法 4. “队列”类算法 5. “递归”类算法 6. “字符串”类算法 7. “树”类算法 8. “哈希”类算法 9. “搜索”类算法 10. “动态规划”类算法 11. “数值分析”类算法 经过8年多的发展,LSGO软件技术团队在「地理信息系统」、「数据统计分析」、「计算机视觉」等领域积累了丰富的研发经验,也建立了人才培养的完备体系,由于自己准备在「量化交易」领域精进技能,如果大家对这个领域感兴趣可以与我联系,加入我们的量化学习群一起学习探讨。 在这个领域我已做了以下积累: 策略部分: 数据部分: 自动化交易部分: |
|