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.

题目大意:求一系列点之间的最大面积

第一种方法:时间复杂度(O(n^2))

 public int maxArea(int[] height) {
int area = 0;
int min = 0;
for(int i=0; i<height.length; i++){
for(int j=i+1; j<height.length; j++){
if(height[i] == height[j])
area = Math.max(area, height[i]*(j-i));
else{
min = Math.min(height[i],height[j]);
area = Math.max(area, min*(j-i));
}
}
}
return area;
}

第二种方法:

 public int maxArea(int[] height) {
int area = 0;
int begin = 0;
int end = height.length-1;
while(begin<end){
area = Math.max(area, (end-begin)*Math.min(height[begin],height[end]));
if(height[begin]<height[end])
begin++;
else
end--;
}
return area;
}

[LeetCode]-011-Container_With_Most_Water的更多相关文章

  1. 【JAVA、C++】LeetCode 011 Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  2. [Leetcode]011. Container With Most Water

    public class Solution { public int maxArea(int[] height) { int left = 0, right = height.length - 1; ...

  3. leetcode python 011

    ####给定n个非负整数a1,a2,...,an,其中每个表示坐标(i,ai)处的点.##绘制n条垂直线,使得线i的两个端点位于(i,ai)和(i,0).##找到两条线,它们与x轴一起形成一个容器,这 ...

  4. 【LeetCode】011 Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  5. [LeetCode] Binary Watch 二进制表

    A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...

  6. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  7. [LeetCode] Restore IP Addresses 复原IP地址

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  8. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  9. [LeetCode] Stickers to Spell Word 贴片拼单词

    We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...

  10. [LeetCode] Bulb Switcher II 灯泡开关之二

    There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...

随机推荐

  1. Luogu P1450 [HAOI2008]硬币购物

    题目 一个很自然的想法是容斥. 假如只有一种硬币,那么答案就是没有限制的情况下买\(s\)的方案数减去强制用了\(d+1\)枚情况下买\(s\)的方案数即没有限制的情况下买\(s-c(d+1)\)的方 ...

  2. mysql 聚合函数(2)

    平均 svg select avg(sal + IFNULL(comm,0)) as avg_sal from t_emp 总和 sum select sum(sal + IFNULL(comm,0) ...

  3. os.path路径拓展 python3

    os.path-对路径path进行的操作 在调用os.path时, 根据操作系统的不同 程序会选择使用posixpath.py或ntpath.py(由os中的代码实现). 对文件命名时应当使用unic ...

  4. 常用的PHP函数封装,有排序和数据库操作函数

    //二分查找 function bin_sch($array, $low, $high, $k) { if ($low <= $high) { $mid = intval(($low + $hi ...

  5. __next__()

    def f1(n): m=n while True: m+=1 yield m a=f1(5) print(a.__next__()) 结果:6

  6. 模板 - 强连通分量/割点/桥 - Tarjan

    int dfn[N], low[N], dfncnt, s[N], tp; int scc[N], sc; // 结点 i 所在 scc 的编号 int sz[N]; // 强连通 i 的大小 voi ...

  7. 2019 Multi-University Training Contest 8 - 1006 - Acesrc and Travel - 树形dp

    http://acm.hdu.edu.cn/showproblem.php?pid=6662 仿照 CC B - TREE 那道题的思路写的,差不多.也是要走路径. 像这两种必须走到叶子的路径感觉是必 ...

  8. node.js使用swig模块

    1.安装swig npm install swig --save 2.创建app.js文件 /*应用程序入口文件*/ /*加载express模块*/ var express = require('ex ...

  9. js将时间戳转化为年月日时分秒

    export const dateFormatter = (nows) => { if (!nows) return '' var now = new Date(nows) var year = ...

  10. js获取两个经纬度之间的角度(0度-360度)

    /** * 获取角度 */mapNumberUtil.getAngle = function(lng_a,lat_a, lng_b, lat_b){ var a = (90 - lat_b) * Ma ...