【LeetCode】11. Container With Most Water 解题报告



转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51820937


Subject

出处:https://leetcode.com/problems/container-with-most-water/

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


Explain

给定n个非负的整数a1,a2 ……an, 去哦中每个代表一个点坐标(i, ai)。一共n个垂直线段。找到两个线段,与X轴形成一个容器,使其能剩最多的水。

其实就是找到这两条线段之后,用最短的线段的长度 * 两个线段之间的距离。


Solution

solution 1

通过嵌套循环来做。很明显,这是比较笨的方法,也是最容易想到的方法。

时间复杂度也就是o(n2)了。

    /**
     * 时间复杂度o(n2)
     *
     * @param height
     * @return
     */
    public int maxArea1(int[] height) {
        if (height == null || height.length < 2) {
            return 0;
        }
        int result = 0;
        int temp = 0;
        for (int i = 0; i < height.length; i++) {
            for (int j = i + 1; j < height.length; j++) {
                temp = (j - i) * Math.min(height[i], height[j]);
                if (temp > result) {
                    result = temp;
                }
            }
        }
        return result;
    }

solution 2

通过两个“指针”,分别指向头和尾。

分别往中间移动,

当 “左指针” 指向的线段长度小于“右指针”指向的线段长度,则移动 “左指针” 。

反之,移动“右指针”。

    /**
     *
     * @param height
     * @return
     */
    public int maxArea2(int[] height) {
        if (height == null || height.length < 2) {
            return 0;
        }
        int left = 0;
        int right = height.length - 1;
        int result = 0;
        int temp = 0;

        while (left < right) {
            temp = (right - left) * Math.min(height[left], height[right]);
            result = Math.max(result, temp);
            if (height[left] < height[right]) {
                left++;
            } else {
                right--;
            }
        }

        return result;
    }

时间复杂度是o(n)。



bingo~~

时间: 2024-10-12 14:58:45

【LeetCode】11. Container With Most Water 解题报告的相关文章

【LeetCode】Container With Most Water 解题报告

[题目] Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a con

LeetCode: Container With Most Water 解题报告

Container With Most WaterGiven n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together wit

Leetcode 11. Container With Most Water (two pointers)

Leetcode: 11 there are two ways to deal with two pointers one is O(n), two pointers moves from both side Another is O(2N), two pointer move from the same side Idea for this, choose the first one and then if there is a smaller one, change that corresp

[LeetCode] 11. Container With Most Water My Submissions Question 解题思路

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

LeetCode 11 Container With Most Water(最大水容器)

翻译 给定n个非负整数a1,a2,...,an,其中每个代表一个点坐标(i,ai). n个垂直线段例如线段的两个端点在(i,ai)和(i,0). 找到两个线段,与x轴形成一个容器,使其包含最多的水. 备注:你不必倾倒容器. 翻译 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such tha

LeetCode 11 Container With Most Water (C,C++,Java,Python)

Problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a

LeetCode 11. Container With Most Water (装最多水的容器)

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

LeetCode 11 Container With Most Water 装尽可能多的水

题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a cont

[LeetCode] 11. Container With Most Water 装最多水的容器

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