Container with Most Water


  • 1

    Click here to see the full article post


  • 1
    K

    When updating the shorter line, no need to check the area until a larger line is found...


  • 0
    B

    when both pointer have same height which one to move?


  • 0
    I

    I think there is a bug, when both pointer have same height, and test cases of Leetcode don't detect that.


  • 1
    Z

    If both pointers have the same height, giving up anyone is fine...


  • 1
    S

    This is very strange but the solution above returns TLE when submitted. Is this a bug in LeetCode test running system? It has happened with me twice.


  • 0
    F

    If the heights are float numbers instead of integer, does the two pointer solution still work?


  • 0
    D

    if both are same, you should move both pointers i.e. l++, r--


  • 0
    U

    the above code is not giving the same area when the input height list is passed in a reverse order. For example, height lists [14, 5, 4] and [4, 5, 14] should give the same max Area formed by the two extreme lines(4, 14) separated by 2 units. The max area is 8. [14,5,4] gives 8 but [4, 5,14] gives 5. Don't you think this is a bug?


  • 0
    U

    I am doing in python, here is the code-

    class Solution(object):
        def maxArea(self, height):
            l = 0
            r=len(height)-1
            res=0
            while l < r:
                area= min(height[l],height[r]*(r-l))
                res = max(res, area)
                if height[l] < height[r]:
                    l+= 1
                else:
                    r -= 1
            return res
    

  • 0
    U

    Sorry, I was doing the mistake, it should be
    min(height[l],height[r]) * (r-l), I was multiplying (r-l) only for second parameter.


  • 0
    F

    O( N log(N) ) passed though.


  • 1
    C

    two pointer is awesome!


  • 1
    U

    THIS IS NOT PROGRAMMING. THIS IS RIDDLE


  • 0
    Y

    when i use #1 run int array {1,2,3...14999,15000},the result is 56250000,but the result is 14999 when i use #2,it is mean that #2 not inclue all the combination


  • 0

    I've never come across a problem of this type before. I wanted to solve it on my own without looking at hints or solution, but couldn't crack it on my own after trying for 40 minutes. Finally gave up and looked at the solution which is pretty simple, surprised why I did not think in this line. Is there anything I can do to improve my problem solving skills, especially the ones which are completely new?


  • 0
    A

    the description of the problem not descriptive and clear !


  • 0
    A

    very clear solution!


  • 0
    L

    I just don't get what this question ask...=。=


  • 1
    V

    @sireesh I think the best way to get better with this type of problem solving is to learn the techniques, in this case the two pointer technique. Keep it in the back of your mind every single future question, and compile a list of problems you couldn't solve the first time and the techniques in the solution. Eventually, most problems will looks like variations of previous problems. Good luck! (p.s. it's a tough road, but we're all in this together :) )


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.