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. MyBatis删除多个类型不一致或不在同一个对象中参数的记录

    控制层中: // 根据店家id查找图书,已售数量要大于等于1才显示 List<SoldBook> sbList = shopService.getSoldBookByShopidAndBo ...

  2. Django查询数据库返回字典dict数据

    个人观点: 个人认为,在Django项目中, 开发团队为了让使用该框架的用户都使用自带的序列化功能,从而让框架中的SQL返回值很不直观,对于直接使用SQL语句的用户很犯难. 解决: from djan ...

  3. c++中的四种智能指针

    c++中的四种智能指针 写惯了python,golang再来写c++总觉得头大,很大一个原因就是他没有一个GC机制. 不过c++中提供了智能指针,也不是不能用,李姐万岁! auto_ptr, shar ...

  4. jackson json序列化 首字母大写 第二个字母需小写

    有这样一个类: @Setter @Getter @JsonNaming(value = PropertyNamingStrategy.UpperCamelCaseStrategy.class) pub ...

  5. 13.AutoMapper 之映射前后(Before and After Map Action)

    https://www.jianshu.com/p/1ff732094f21 映射前后(Before and After Map Action) 你可能偶尔需要在映射发生前后执行自定义逻辑.这应该很少 ...

  6. redis为什么使用单线程 ,还那么快,单线程是怎么实现的

    单线程使用队列 为什么使用单线程 https://baijiahao.baidu.com/s?id=1628498089535886382&wfr=spider&for=pc http ...

  7. Ubantu 手动设置DSL连接

    参考链接:https://m.linuxidc.com/Linux/2015-07/119774.htm

  8. js 学习三 Array

    1.数组的长度 var sequence = [1, 1, 2, 3, 5, 8, 13]; sequence .length //7 2.字符串转换成数组 string.split() var my ...

  9. Ant 学习

    到了新公司,发现公司使用ant 来代码生成.本来学习后写下来.在网上找到一篇教程,实在是非常给力... 就把连接记下来吧:http://www.blogjava.net/amigoxie/archiv ...

  10. 关于session和cookie的区别

    以前对于session和cookie的认识,就只是粗略的知道cookie保存在客户端,而session则保存在服务端. 如今查了些资料,对session和cookie也有了一个初步的认识,现在来总结一 ...