Go and PHP calculate the container with the most water based on a set of numbers

Time:2021-7-22

Original link: go letcode, author: San Jin and his meow PHP code

Container with most water

The main questions are as follows:

Let’s give you n non negative integers A1, A2,…, an, each of which represents a point (I, AI) in the coordinates. Draw n vertical lines in the coordinates, and the two ends of the vertical line I are (I, AI) and (I, 0). Find out two of the lines so that the container that they make up with the x-axis can hold the most water.
Note: you cannot tilt the container, and the value of n is at least 2.
Go and PHP calculate the container with the most water based on a set of numbers
The vertical line in the figure represents the input array [1,8,6,2,5,4,8,3,7]. In this case, the maximum value that the container can hold water (represented by the blue part) is 49.
Example:
   Input: [1,8,6,2,5,4,8,3,7]
   Output: 49
Source: power button

According to the meaning of the topic, the topic is abstracted as a mathematical model: given aArrayandTwo indexes x, y, on the moveX、YIn the process of solving the problemS = |Y-X| * Min( array[X], array[Y] )WhereMin (array [x], array [y]) denotes H

  1. |Y-X | is the absolute value of index difference
  1. Min (array [x], array [y]), because under the premise of | Y-X | fixed, the water capacity of the container completely depends on the smaller height, so this side takes min.

Thinking of solving problems

We willX points to the first position in the arrayY points to the last position of the array。 Calculate and record the value of s according to the above formula. Determine the values of array [x] and array [y], ifIf array [x] > array [y], y moves to the left; if array [x] < array [y], X moves to the right; if array [x] = array [y], X moves to the right and Y moves to the left。 In short, the values of the elements pointed to by the index move with smaller values. If they are equal, they move together. The explanation is as follows:

Suppose that the element x points to is 2 and the element Y points to is 7. Because 2 is smaller than 7, X moves to the right. As for why y does not move to the left, it is because the value of H is the smaller of array [x] and array [y]. If y is moved to the left, assuming that array [Y-1] is larger than array [x], then the value of H is still array [x], while the value of (Y-X) is smaller, so s is smaller. Suppose that array [Y-1] is smaller than array [x], then the value of H is array [Y-1], smaller than before, and the value of (Y-X) is smaller, so s is smaller. So move the index with a smaller element value. If array [x] and array [y] are equal, then x and Y move together, and the analysis process is consistent with inequality.

The flow chart analysis is as follows:
Go and PHP calculate the container with the most water based on a set of numbers

Go implementation:

func maxArea(height []int) int {
    var (
        i         = 0
        j         = len(height) - 1
        max       = 0
        minHeight = 0
    )

    for i < j {
        //Calculation of H
        if height[j] > height[i] {
            minHeight = height[i]
        } else {
            minHeight = height[j]
        }

        //Record area, that is, water capacity
        newMax := (j - i) * minHeight
        if newMax > max {
            max = newMax
        }

        //Move the small pointer, if equal, move together
        if height[j] > height[i] {
            i++
        } else if height[j] < height[i] {
            j--
        } else {
            j--
            i++
        }
    }
    return max
}

PHP implementation:

function maxArea($nums) {
    $i= 0;
    $j = count($nums) - 1;
    $max = 0;
    while ($i < $j) {
        if ($nums[$j] > $nums[$i]) {
            $minHeight = $nums[$i];
        } else {
            $minHeight = $nums[$j];
        }

        //Recording area
        $newMax = ($j - $i) * $minHeight;
        if ($newMax > $max) {
            $max = $newMax;
        }

        //Move the small pointer, if equal, move together
        if ($nums[$j] > $nums[$i]) {
            $i++;
        } elseif ($nums[$j] < $nums[$i]) {
            $j--;
        } else {
            $j--;
            $i++;
        }
    }

   return $max;
}

echo maxArea([1,8,6,2,5,4,8,3,7]); // output: 49

The last routine with goods courses to buy a wave

The article first appeared in he Xiaodong’s blog