[Leetcode]11.container with the most water

Source: Internet
Author: User

"title"

GivenNnon-negative integersa1 ,a2 , ...,an , where each represents a point at coordinate (I,ai ).NVertical lines is drawn such that the both endpoints of lineIis at (I,ai ) and (I, 0). Find lines, which together with X-axis forms a container, such that the container contains the most water.

Note:you may not slant the container.

"Analysis One"

The most straightforward idea is the brute force enumeration, which enumerates the two sides of a container. O (n^2). Timeout.

"Code One"

/********************************** Date: 2015-01-21* sjf0115* title: 11.Container with the most water* website: Https://oj.leetco de.com/problems/container-with-most-water/* Result: Time Limit exceeded* Source: leetcode* Blog: ******************************* /#include <iostream> #include <algorithm> #include <vector>using namespace Std;class Solution {        Public:int Maxarea (vector<int> &height) {int count = Height.size ();        if (count <= 1) {return 0;        }//if int maxarea = 0;                Left bounds for (int i = 0;i < Count-1;++i) {//Right boundary for (int j = I+1;j < Count;++j) {                int area = (j-i) * min (height[i],height[j]);            Maxarea = max (Area,maxarea);    }//for}//for return maxarea;    }};int Main () {solution solution;    Vector<int> VEC;    Vec.push_back (2);    Vec.push_back (3);    Vec.push_back (1);    Vec.push_back (4);    Vec.push_back (2); int result = SOlution.maxarea (VEC);    Output cout<<result<<endl; return 0;}


"Analysis II"

When considering from both sides to the middle, the area = the width between the two sides of the smaller height x. Assuming that the initial water area is area=0, if the left height is less than the right height, the left side of the container moves one position to the right. Similarly, if the height on the right is less than the height to the left, move one bit to the left.

"code two"

/********************************** Date: 2015-01-21* sjf0115* title: 11.Container with the most water* website: Https://oj.leetco de.com/problems/container-with-most-water/* Result: ac* Source: leetcode* Blog: **********************************/#include <iostream> #include <algorithm> #include <vector>using namespace Std;class solution {Public:int Maxar        EA (vector<int> &height) {int count = Height.size ();        if (count <= 1) {return 0;        }//if int maxarea = 0,area = 0;                Binary find variant for (int i = 0,j = Count-1;i < J;) {//Remove low side if (Height[i] > Height[j]) {                Area = (j-i) * HEIGHT[J];            --j;                }//if else{area = (j-i) * Height[i];            ++i;            }//else//Update maximum size if (Area > Maxarea) {maxarea = areas;    }//if}//for return maxarea; }};int Main () {solution Solution;    Vector<int> VEC;    Vec.push_back (2);    Vec.push_back (3);    Vec.push_back (1);    Vec.push_back (4);    Vec.push_back (2);    int result = Solution.maxarea (VEC);    Output cout<<result<<endl; return 0;}


"Analysis of the three"

When considering from both sides to the middle, the area = the width between the two sides of the smaller height x. Assuming that the initial water area is area=0, if the left height is less than the right height, the container left moves to the right until it finds a height higher than the original left. Similarly, if the height on the right is less than the height on the left, move to the left until you find a height higher than the previous right.

"code three"

Class Solution {Public:int Maxarea (vector<int> &height) {int count = Height.size ();        if (count <= 1) {return 0;        }//if int index,maxarea = 0,area = 0;                Binary find variant for (int i = 0,j = Count-1;i < J;) {//Remove low side if (Height[i] > Height[j]) {                Area = (j-i) * HEIGHT[J];                index = j;                Move left until you find a while (Height[j] <= Height[index]) higher than Height[index] {--j;                }//while}//if else{area = (j-i) * Height[i];                index = i;                Move right until you find a while (Height[i] <= Height[index]) higher than Height[index] {++i;            }//while}//else//Update maximum area if (Areas > Maxarea) {maxarea = space;    }//if}//for return maxarea; }};


"Analysis Four"

The build structure contains the position of height and height in the original array.

struct Node
{
int height;
int index;

};

The array of the struct is incremented by the value of height, assuming that the sorted array is VEC.

Suppose F[i] represents an array vec[i,i+1,...] The maximum amount of water after all the height is arranged in the original position order

Then f[i-1] can be calculated in O (1) Time: Because Vec[i-1].height is less than vec[i,i+1,...] All the height within, so the container height of Vec[i-1].index is vec[i-1].height, the maximum amount of water needs to be calculated separately vec[i,i+1,...] The top and back height are arranged in the original position, and the larger value of both is taken. That is, black is the lowest, to calculate the maximum amount of water for a container with black boundaries, it is only necessary to separate and first and last calculation, to go to both the larger values


"code Four"

Class Solution {struct Node {int height;        int index;            Node (int h, int i): height (h), index (i) {} Node () {} BOOL operator < (const Node &a) Const {        return height < a.height;        }};p ublic:int Maxarea (vector<int> &height) {int res = 0, n = height.size ();        if (n <= 1) return 0;        Vector<node>vec (n);            for (int i = 0; i < n; i++) {vec[i].index = i;        Vec[i].height = Height[i];                 } sort (Vec.begin (), Vec.end ());            int start = Vec[n-1].index, end = start;//records the left and right ends of the original position of the height that has been processed for (int i = n-2; I >= 0; i--) {            Start = Min (start, vec[i].index);            end = Max (end, Vec[i].index);        res = max (res, Max (vec[i].height* (Vec[i].index-start), vec[i].height* (End-vec[i].index)));    } return res; }};



Similar topics:

The largest rectangular area in the histogram of the Pango network

Leetcode's trapping Rain water



[Leetcode]11.container with the most water

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.