[leedcode 11] Container With Most Water

public class Solution {
    public int maxArea(int[] height) {
       /* 题意:二维坐标系里有 n 个点 (i, ai), ai >= 0,从 (i, ai)到(i, 0)划竖线,共有 n 条竖线。
        找出两条竖线,使得它们构成的矩形的面积最大,矩形的高取决于最短的竖线。
        思路:贪心
        从首尾两个下标head 和trail 处开始扫描,用一个变量 maxArea 保持当前最大的矩形面积。
        如果head 指向的竖线短于 trail 的,则右移 head
        否则左移 trail
        计算面积,更新 maxArea
        复杂度:时间O(n),空间O(1)*/
        if(height.length<2) return 0;
        int left=0;
        int right=height.length-1;
        int res=0;
        while(left<right){
           res=Math.max( res,Math.min(height[left],height[right])*(right-left));
           if(height[left]<height[right]){
               left++;
           }else
               right--;

        }
        return res;

    }
}
时间: 2024-11-05 12:31:49

[leedcode 11] Container With Most Water的相关文章

11. Container With Most Water【leetcode】,java算法,数组,求最大水池注水量

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 togethe

刷题11. Container With Most Water

一.题目说明 11.Container With Most Water,这个题目难度是Medium. 二.我的做法 乍一看,简单啊,两个for循环就可以了,我在本地写的. #include<iostream> #include<vector> #include<math.h> using namespace std; class Solution{ public: int maxArea(vector<int> &height){ int max =

[leecode]---11.container with most water

description: Input: [1,8,6,2,5,4,8,3,7]Output: 49 思路1: 从(1,a1)开始向后算面积,需要两层n循环,时间复杂度n2 思路2: 找出数组中最大的数,将其与次大,第三大数求面积,依次类推,也需要两层循环,还需要额外排序,时间复杂度n2 因为找出最大数并且并不知道输入数据的规律(有可能很杂乱),所以每个都有必要算,采取思路1. 代码实现如下: class Solution { public int maxArea(int[] height) {

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 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 (装最多水的容器)

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 contain

【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