C + + Digital container water problem (a)---"Those strange Algorithms" __web

Source: Internet
Author: User

Reference: "One-day-one-leetcode"
Problem Description:

Note that the container water in the program depends on the small side of the container, not the big side of the container.

Optimizing the Implementation code:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int Maxarea (vector<int> height) {
    int i = 0;
    Int J = height.size ()-1;
    int max = 0;
    while (J > i) {
        int con = 0;
        int min = height[i] > height[j]? HEIGHT[J]: height[i];
        if (j = = i + 1) con = min;
        else con = min* (j-i);
        if (Con > Max) {
            max = con;
        }

        Move the left and right pointers, and note that the smallest we have calculated
        if (Height[i] > Height[j]) {
            j--
        }
        else{
            i++
        }
    }
    return max;
}

int main () {
    vector<int> v;
    int arr[] = {3, 4, 5, 6, 9, ten};
    for (int i = 0; i < sizeof (arr)/sizeof (arr[0)); i++) {
        v.push_back (arr[i]);
    int ma = Maxarea (v);
    cout << ma << endl;
    return 0;
}

Run Result:

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.