Golang O(n)


  • 0
    A

    We keep searching the better answer from the case, which we take the first line and the last line.
    And then, we move 2 edges close step by step.
    The better answer have to be with 2 longer lines, so when (h[i] < h[j]) ? i++ : j-- (i<<j)

    func maxArea(height []int) int {
    	i := 0
    	j := len(height) - 1
    	retVal := 0
    	for i < j {
    		if height[i] > height[j] {
    			if retVal < height[j]*(j-i) {
    				retVal = height[j] * (j - i)
    			}
    			j--
    		} else {
    			if retVal < height[i]*(j-i) {
    				retVal = height[i] * (j - i)
    			}
    			i++
    		}
    	}
    	return retVal
    }
    

Log in to reply
 

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