Leetcode 11. Container With Most Water

This kind of question is definitely not violent. Think about whether there is a way to solve it after traversing it once.

 

The key to this inversion is that the final area is bounded by the shorter side.

First set two variables, which are the two boundaries of the rectangle, and iterate inward in turn.

Only the short side is needed for each iteration, because if the long side is iterated, the new area must be smaller than the area of ​​the previous step (new high <= short side, new base < original low). If both sides are equal, there is actually no need to proceed.

The time complexity is O(n).

 

The picture explains more clearly: https://leetcode.com/articles/container-most-water/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325002597&siteId=291194637