Nine Chapters count judges Net-original website
http://www.jiuzhang.com/problem/45/
Topics
Given a positive integer array (a0,a1.), representing n coordinates (0,A0), (1,A1), the N-points are drawn according to the n Dots, and the two endpoints of each segment are (I, AI) and (i, 0) respectively. Two line segments are found so that the container of the two segments and the x-axis has the largest capacity for storing water. such as [2,1,3], maximum, select the first segment and the third segment, plus the x-axis of the container, the water storage capacity of 4 (height of min (2,3) = 2, bottom is 2)
Answer
With two pointers, one pointing to the head, one pointing to the tail, if A[head] > A[tail], means that the maximum container area to the right of tail is a[tail] * (tail-head), record this value, and then throw away the tail line. Similarly if a[head] < A[tail] We can record A[head] * (Tail-head), and then throw away head. Repeat the above algorithm to know that head meets tail. The maximum value in the area recorded in the process is the problem. Time complexity O (n), Space O (1)
Interviewer Angle
In the 42nd and 43 of the nine chapters, we mentioned the use of stacks to aid in calculating the number of "first" numbers that go to the left or to the number of small or large. In this problem we are not difficult to analyze, we need to look for each number to the left or toward the right number of the "last" number. Then this is not in accordance with the scenario we used to use the stack. It is therefore necessary to find other solutions. The problem with two pointers is that you use more ideas in the array interview questions. In future topics, we will also introduce the use of similar ideas in the topic.
Nine-chapter algorithm surface test 45 to find the largest water storage container