`

LeetCode 11 - Container With Most Water

 
阅读更多

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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.

public int maxArea(int[] height) {
    int l=0, r=height.length-1;
    int max = 0;
    while(l<r) {
        int area = Math.min(height[l], height[r])*(r-l);
        max = Math.max(max, area);
        if(height[l] >= height[r]) {
            r--;
        } else {
            l++;
        }
    }
    return max;
}

 

分享到:
评论

相关推荐

    [LeetCode] Container With Most Water(正确的算法)1

    [LeetCode] Container With Most WaterGiven n non-negative integers a1, a2, ..., a

    [LeetCode]Container With Most Water(中期有问题仅供参考)1

    /*中级思路以下我觉得分析有点问题*/ 中级思路, 这样的值 有两个性质, 不妨设 (i,a)为左边 (j,b)为右边为最大值, 1.若有坐标

    leetcode答案-Leetcode:力码

    11 Container With Most Water 这里题目的分析图如下: 所以我们需要找的是ai,aj中的最小值作为高,然后找(i-j)的最大值作为长 然后得到最大的容积。 所以我们这样做 用两个point来记录现在所在的x坐标(即i值) ...

    leetcode叫数-leetcode:leetcode

    with Ruby 13. Roman to Integer 查表,通过从前往前筛选字符串,把代表的值一个个加起来 26. Remove Duplicates from Sorted Array 难度easy的题目。根据题目要求,是不能新建数组。只能在原来的基础上做修改。基本...

    leetcode338-LeetCode:LeetCode刷题总结

    11.Container With Most Water 12.Integer to Roman 13.Roman to Integer 14.Longest Common Prefix (Trie树待完成) 15.3Sum 16.3Sum Closest 17.Letter Combinations of a Phone Number 18.4Sum 19.Remove Nth Node...

    leetcode跳跃-LeCode:乐科

    leetcode 跳跃 LeetCode Solved by Python easy/middle/hard:15/36/5 1. Two Sum 两数之和 2. Add Two Numbers 两数相加 3. Longest Substring Without Repeating Characters 无重复字符的最长子串 4. Median of ...

    leetcode530-algorithm:算法

    leetcode 530 ** LeetCode 题目更新 ** 用来记录业余时间所做的算法题目,保持对于数据结构的熟悉。 ** Leetcode 题目列表 005 Longest Palindromic Substring 006 ZigZag Conversion 007 Reverse Integer 008 ...

    颜色分类leetcode-leetcode-[removed]我对Leetcode问题的解决方案

    颜色分类leetcode My Leetcode Problems Solutions Using javascript(ES6) 1 Two Sum 两数之和 5 Longest Palindromic Substring 最长回文子串 7 Reverse Integer 整数反转 9 Palindrome Number 回文数 11 Container...

    leetcode答案-LeetCode:我的力扣解决方案

    leetcode 答案 LeetCode My LeetCode solution List 4. Longest Substring Without Repeating Characters: Using the dict[] to record the last position of each letter. 5. Longest Palindromic Substring: ...

    leetcode下载-algorithm_practise:算法实践

    leetcode下载 algorithm_reearch algorithm_reearch 链表 leetcode: 146 有无环 双向链表,删除 写大于读的情形 LinkedHashMap与 LRU算法 基于访问时间的链表 或者 基于插入时间的链表 哈希表: leetcode: 1. two sum...

    分割数组求最大差值leetcode-Leetcode-Road:LeetCode刷题记录

    分割数组求最大差值leetcode LeetCode 学习之路 记录自己完成LeetCode的代码和结果。 序号 中文名称 英文名称 通过率 难度 1 Two Sum 47.0% 简单 2 Add Two Numbers 36.0% 中等 3 Longest Substring Without ...

    leetcode中国-algo:数据结构和算法

    leetcode中国 algo Data Structure & Algorithm 目录结构 【php】 使用php实现常见的算法与数据结构 【leetcode】 使用python实现leetcode middle级的题目 Array(数组) ID Difficulty Title Python 1 Easy Two Sum ...

    javalruleetcode-LeetCode:LeetCode算法问题

    Container With Most Water LeetCode 19 Remove Nth Node From End of List LeetCode 42 Trapping Rain Water LeetCode 61 RotateList LeetCode 75 Sort Colors LeetCode 125 Valid Palindrome LeetCode 167 Two Sum...

    leetcode题库-little-algorithm:LeetCode题目参考代码与详细讲解,公众号《面向大象编程》文章整理

    leetcode题库 Little Algorithm 从 2020 年初开始,我在公众号《面向大象编程》上发表面试算法、LeetCode 题解相关文章,至今收获不少好评。此仓库是公众号内容的补充,包括公众号文章涉及到的题目的参考代码,以及 ...

    leetcode不会-Front-end-Engineering:前端工程师常用数据结构、算法和资源列表

    ContainerWithMostWater(中) MaxBuySell(中) MaxSlidingWindow(硬) MaxSumNonAdjacent(中) 最小窗口子串(硬) 重叠时间(中) ProductWithoutSelf(硬) 旋转阵列(中) SpiralMatrix(简单) 指针: 向左...

    LeetCode-in-Golang:使用 Golang 解答 LeetCode

    35%2☆ ☆27%3☆ ☆24%4☆ ☆ ☆21%5☆ ☆25%6☆ ☆26%7☆24%8☆ ☆13%9Palindrome Number☆35%10Regular Expression Matching☆ ☆ ☆24%:red_heart:11Container With Most Water☆ ☆36%12Integer to R

    leetcode-go:我使用Golang解决LeetCode问题的方法

    leetcode-goMy solution to LeetCode problems using GolangProblems 题库Array 数组NoTitle题名DifficultyStatus11Container With Most Water盛最多水的容器MediumSolved26Remove Duplicates from Sorted Array删除...

    程序员面试宝典LeetCode刷题手册

    11. Container With Most Water 13. Roman to Integer 15. 3Sum 16. 3Sum Closest 17. Letter Combinations of a Phone Number 18. 4Sum 19. Remove Nth Node From End of List 20. Valid Parentheses 21. Merge Two...

    javalruleetcode-LeetCode::lollipop:个人LeetCode习题解答仓库-多语言

    Container with Most Water 14 Longest Common Prefix 15 Three Sum 16 Three Sum Closest 20 Valid Parentheses 26 Remove Duplicates from Sorted Array 48 Rotate Image 53 Maximum Subarray 55 Jump Game 56 ...

    leetcode2sumc--Offer:-提供

    leetcode 2 sum c LeetCode 贵有恒,何必三更起五更睡;最无益,只怕一日暴十寒。 我的个人网站: 分享技术,乐享生活:Jack Cui公众号每周五推送“程序员欢乐送”系列资讯类文章,欢迎您的关注! 帮助文档 帮助文档...

Global site tag (gtag.js) - Google Analytics