This is the ninth day of my participation in the August More text Challenge. For details, see: August More Text Challenge
🚺 topic
I give you n non-negative integers A1, A2… Each number represents a point (I, ai) in the coordinates. N vertical lines are drawn in the coordinates. The two endpoints of vertical line I are (I, ai) and (I, 0). Find two of these lines so that they, together with the X-axis, can hold the most water in the container.
Note: You cannot tilt a container.
Example 1:
Input:1.8.6.2.5.4.8.3.7] output:49Explanation: The vertical line represents the input array [1.8.6.2.5.4.8.3.7]. In this case, the maximum that the container can hold water (shown in blue) is49.Copy the code
The sample2: Input: height = [1.1] output:1
Copy the code
The sample3: Input: height = [4.3.2.1.4] output:16
Copy the code
The sample4: Input: height = [1.2.1] output:2
Copy the code
Tip: n = height.length2 <= n <= 3 * 104
0 <= height[i] <= 3 * 104Through the number of times497.423Submit the number785.189
Copy the code
🚺 A little thought
See this problem I feel the brush problem is not white brush ah, think of a few days ago we do together “no repeated character of the most eldest son string” we were using a double pointer to simulate the sliding window mechanism also remember? I don’t know if you’ve heard of the short board link but I want to use the Force to solve this problem and every time the pointer moves we just move the short board. Come on, let’s see what the solution is.
Open dry 🚺
🚺 describes a function
So let’s go back to the Math function because it’s very similar to what we used before
Math.E Is a constant that records E. There are some similar constants in Math that are often used in engineering mathematics. Math.abs Take the absolute value math.sin Math.asin arcsine math.cos math.acos Arccosine math.tan Tangent math.atan Arctangent math.atan2 [color=red] [color=red] [color=red] [color=red] [color=red] math. floor [/color] Math.IEEEremainder [/color] Math. Max [/color] Math.min [/color] Math. SQRT [/color] Math. SQRT [/color [color=red] math. pow Raise a number to any power and throw ArithmeticException handle overflow exception [/color] math. exp Raise e to any power math.log1010[color=red] math.round [color=red] math.round [color=red] math.round [color=red] math.round [color=red] math.round [color=red] math.round [color=red] math.roundintType orlongType (returned by the previous functiondoubleType) [/color] math.random returns0.1Between a random numberCopy the code
🚺 source code and analysis process
Height [I]&&height[j] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
class Solution {
public int maxArea(int[] height) {
// Set I to the left and j to the right
int i = 0, j = height.length - 1, res = 0;
// When both sides of the container are in the same position, the bottom edge of the container is zero
while(i < j){
res = height[i] < height[j] ? Math.max(res, (j - i) * height[i++]): Math.max(res, (j - i) * height[j--]);
}
returnres; }}Copy the code
Now let’s look at this
res = height[i] < height[j] ? Math.max(res, (j - i) * height[i++]): Math.max(res, (j - i) * height[j--]);
Copy the code
If you don’t know the difference between I ++ and ++ I, please remember to chat with me privately, or leave a message and I will fix it for you. Select * from ‘I’ where height[I]&&height[j] where height[I] is small move I ++ to the right and the other side is the same. And then you compare it to the original number and if it’s bigger than the original res, you change it. Well, that’s the end of today’s practice. See you tomorrow.