zoukankan      html  css  js  c++  java
  • 11. Container With Most Water

    Given n non-negative integers a1a2, ..., an , where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

    Note: You may not slant the container and n is at least 2.

    The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

    Example:

    Input: [1,8,6,2,5,4,8,3,7]
    Output: 49

    AC code:

    class Solution {
    public:
        int maxArea(vector<int>& height) {
            int count = 0;
            int ans, num;
            int len = height.size();
            vector<int> v;
            for (int i = 0; i < len; ++i) {
                num = 0;
                for (int j = len-1; j > i; --j) {
                    if (height[j] >= height[i]) {
                        ans = height[i] * (j - i);
                        num = max(num, ans);
                        break;
                    } else {
                        ans = height[j] * (j - i);
                        num = max(num, ans);
                        continue;
                    }
                }
                count++;
                v.push_back(num);
            }
            sort(v.begin(), v.end());
            return v[count-1];
        }
    };
    

    Runtime: 472 ms, faster than 24.96% of C++ online submissions for Container With Most Water.


    when i find the discuss, I find a way to improve the code's effectiveness.

    The O(n) solution with proof by contradiction doesn't look intuitive enough to me. Before moving on, read the algorithm first if you don't know it yet.

    Here's another way to see what happens in a matrix representation:

    Draw a matrix where the row is the first line, and the column is the second line. For example, say n=6.

    In the figures below, x means we don't need to compute the volume for that case: (1) On the diagonal, the two lines are overlapped; (2) The lower left triangle area of the matrix is symmetric to the upper right area.

    We start by computing the volume at (1,6), denoted by o. Now if the left line is shorter than the right line, then all the elements left to (1,6) on the first row have smaller volume, so we don't need to compute those cases (crossed by ---).

      1 2 3 4 5 6
    1 x ------- o
    2 x x
    3 x x x 
    4 x x x x
    5 x x x x x
    6 x x x x x x
    

    Next we move the left line and compute (2,6). Now if the right line is shorter, all cases below (2,6) are eliminated.

      1 2 3 4 5 6
    1 x ------- o
    2 x x       o
    3 x x x     |
    4 x x x x   |
    5 x x x x x |
    6 x x x x x x
    

    And no matter how this o path goes, we end up only need to find the max value on this path, which contains n-1 cases.

      1 2 3 4 5 6
    1 x ------- o
    2 x x - o o o
    3 x x x o | |
    4 x x x x | |
    5 x x x x x |
    6 x x x x x x
    

    Hope this helps. I feel more comfortable seeing things this way.

    code:

    class Solution {
    public:
        int maxArea(vector<int>& height) {
            int count = 0;
            int ans, num;
            int len = height.size();
            int j = len-1;
            vector<int> v;
            for (int i = 0; i < len; ++i) {
                num = 0;
                for (; j > i; --j) {
                    if (height[j] >= height[i]) {
                        ans = height[i] * (j - i);
                        num = max(num, ans);
                        break;
                    } else {
                        ans = height[j] * (j - i);
                        num = max(num, ans);
                        continue;
                    }
                }
                count++;
                v.push_back(num);
            }
            sort(v.begin(), v.end());
            return v[count-1];
        }
    };
    

    Runtime: 16 ms, faster than 53.39% of C++ online submissions for Container With Most Water.

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    【转】70个经典的 Shell 脚本面试问题
    【转】最牛B的编码套路
    【转】Flex 布局语法教程
    【转】程序员7大软技能测验 你得几分?
    【转】为什么事务日志自动增长会降低你的性能
    【hive】——metastore的三种模式
    【hive】——Hive基本操作
    【hive】——Hive初始了解
    【hive】——Hive四种数据导入方式
    【hive】——Hive sql语法详解
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9727941.html
Copyright © 2011-2022 走看看