Sheng PHP algorithm of the most water containers

Given n non-negative integers a1, a2, ..., an, a point (i, ai) each represents the number of coordinates. Videos n lines in vertical coordinate, i is a vertical line two end points are (i, AI) and (i, 0). Find out the two lines, which together with the container so that the x-axis configuration can accommodate up water.

Note: You can not tilting the container, and the value of n is at least 2.

 

FIG vertical line represents the input array [1,8,6,2,5,4,8,3,7]. In this case the maximum container capable of holding water (indicated as blue) of 49.

 

Example:

Input: [1,8,6,2,5,4,8,3,7]
Output: 49

Source: stay button (LeetCode)

class Solution {

    /**
     * @param Integer[] $height
     * @return Integer
     */
    function maxArea($height) {
        $strlen = count($height);
        $r = $strlen -1;
        $l = 0;
        $arr = [];
        
        for($i=$r;$i>0;$i--){

            $val = min($height[$l],$height[$r])*$i;

            if ($height[$l] < $height[$r]) {
                ++$l;
            } else {
                --$r;
            }

            $ Arr [] = $ val;

        }
        return max($arr);
    }
}

Guess you like

Origin www.cnblogs.com/corvus/p/11968234.html