Nine-chapter algorithm surface test 45 to find the largest water storage container

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.