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)
  • 相关阅读:
    开源.net 混淆器ConfuserEx介绍
    k8s删除namespace失败,状态Terminating解决方案
    java get all threadlocal from thread
    mysql查看索引的大小
    InnoDB一定会在索引中加上主键吗?
    全链路追踪traceId,ThreadLocal与ExecutorService
    redis 批量删除keys
    shell逐行读取excel并执行sql
    Is it possible to create @Around Aspect for feign.Client
    Spring Boot后台启动不打印nohup.out
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9727941.html
Copyright © 2011-2022 走看看