[Brush questions diary] 11. The container that holds the most water

Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-In Event, click to view the event details .

[Brush questions diary] 11. The container that holds the most water

The thirteenth chapter of this writing diary is titled: 11. The container that holds the most water , medium

1. Topic description:

How does it feel to get this question?

There are few words in the title description, and it seems to be quite clear. Isn't it just to calculate the maximum amount of water that the container can hold, but I feel that the more clear the title is, the more confusing it will be to realize it?

It's okay guys, let's just play and analyze it

2. Thought analysis:

1. What idea does this question examine? What is your thinking?

Let's take a look at what important information this question gives us:

  • An array will be given in the question, and the number of elements in this array is 2 - 10 to the 5th power, which means that we do not need to judge whether the length of the input array is less than 2.
  • Convert the array into a container, calculate the width and height, and get the maximum amount of water that the container can hold

This is a bit like the barrel effect , how much water a barrel can hold depends on the shortest board, and here, how much water a container can hold depends on 2 variables

  • the height of the container
  • the width of the container

The combined need to achieve the height of the container * the width of the container is the largest

So let's deduce:

Example given using the title : [1,8,6,2,5,4,8,3,7]

For this question, we want to calculate the maximum size of the water that the container can hold, so we can use a box to make a box selection. Would you make a box selection like this? Expand from left to right to the right to make a box selection

Take the first column as the standard and expand to the right

Omit next steps...

Take the second column as the criterion and expand to the right

Omit next steps...

According to the above method, it is possible to find the maximum amount of water we expect to find , but the time complexity of this method is too high, and it will inevitably time out. We can change our thinking.

Since it is to find the maximum amount of water, can we start from the data boundary that may be the largest? We can't control the height of the column, but we can control the width of the container

Therefore, we can find

  • Directly from the leftmost column and the rightmost column as the border of the container to continuously shrink to the middle
  • When it needs to be folded, start from the shorter side , so that it is easier to understand, and the number of cycles can be greatly reduced.

The above figure has clearly simulated the process of boundary closing, and the rest is that we will realize the above ideas together, using the method of double pointers

3. Coding

According to the above logic and analysis, we can translate it into the following code. According to the meaning of the question, we do not need to judge whether the length of the given array is less than 2.

The encoding is as follows:

func maxArea(height []int) int {
	// 赋初始值,定位容器的左边界和右边界 (双指针的方法)
    i , j := 0,len(height) - 1

    res := 0
    for i<j {
        // 计算当前边界的容器面积,取决于较矮的一边
        res = max(res,(j-i) * min(height[i],height[j]))
		// 从较短的那一边开始收拢
        if height[i] < height[j]{
            i++
        }else{
            j--
        }
    }
    return res
}

func max(a,b int)int{
    if a>b {
        return a
    }
    return b
}

func min(a,b int)int{
    if a<b {
        return a
    }
    return b
}
复制代码

The above is the code implemented according to the idea. The idea is clear. Coding is a translation process. Considering various scenarios, we can implement it more smoothly, which can reduce the risk of rework and bugs.

4. Summary:

Here we only need to traverse the entire array once, the time complexity is O(n), and the space complexity is O(1), because we are all using constant-level space consumption

Original title address: 11. The container that holds the most water

I am here today, what I have learned, if there are any deviations, please correct me

Welcome to like, follow, favorite

Friends, your support and encouragement are the motivation for me to persist in sharing and improve quality

Okay, here it is this time

Technology is open, and our mentality should be open. Embrace change, live in the sun, and move forward.

I'm the little devil boy Nezha , welcome to like, follow and collect, see you next time~

Guess you like

Origin juejin.im/post/7079697653612838948