原题链接:https://leetcode.com/problems/container-with-most-water/description/

题目要求:给定n个非负整数a1,a2,...,an ,每一个整数对应一个坐标(i,a)。以(i,0)和(i,a)为端点画一条线段,现在选择两条线段,以x轴为底构成一个容器,请问选择那两条线段时可以使容器盛更多的水?

注意:“短板效应”,容量取决于短的那条边,以及两条线段之间的距离

思路:求出Max[min(line1,line2)*(i1-i2)]

方法一:暴力解决,去任意两条不同线段构成容器,求出最大值。时间复杂度O(n2),空间复杂度O(1)。

方法二:两端逼近,从数组的两端开始,不断向中间逼近。时间复杂度O(n),空间复杂度O(1)

 package com.huiAlex;

 import java.util.Random;

 public class ContainerWithMostWater11 {

     public static void main(String[] args) {
int[] height = new int[10];
Random ran = new Random();
for (int i = 0; i < height.length; i++) {
height[i] = ran.nextInt(10);
System.out.println(height[i]);
}
ContainerWithMostWater11 cw = new ContainerWithMostWater11();
int a = cw.maxArea(height);
int b = cw.maxArea2(height);
System.out.println("area: " + a);
System.out.println("area2:" + b);
}
// 两端逼近
public int maxArea(int[] height) {
int maxarea = 0;
int l = 0;
int r = height.length - 1;
while (1 < r) {
maxarea = Math.max(maxarea, Math.min(height[l], height[r]) * (r - l));
if (height[l] < height[r])
l++;
else
r--;
}
return maxarea;
}
// 暴力解决
public int maxArea2(int[] height) {
int maxarea = 0;
for (int i = 0; i < height.length; i++)
for (int j = i + 1; j < height.length; j++)
maxarea = Math.max(maxarea, Math.min(height[i], height[j]) * (j - i));
return maxarea;
} }

LeetCode:11. ContainerWithWater(Medium)的更多相关文章

  1. LeetCode:18. 4Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/4sum/description/ 2. 题目要求 给出整数数组S[n],在数组S中是否存在a,b,c,d四个整数,使得四个 ...

  2. LeetCode:15. 3Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/3sum/description/ 2. 题目要求 数组S = nums[n]包含n个整数,请问S中是否存在a,b,c三个整 ...

  3. LeetCode:46. Permutations(Medium)

    1. 原题链接 https://leetcode.com/problems/permutations/description/ 2. 题目要求 给定一个整型数组nums,数组中的数字互不相同,返回该数 ...

  4. LeetCode: 60. Permutation Sequence(Medium)

    1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...

  5. LeetCode: 61. Rotate List(Medium)

    1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...

  6. LeetCode: 62. Unique Paths(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...

  7. LeetCode: 55. Jump Game(Medium)

    1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的 ...

  8. LeetCode: 54. Spiral Matrix(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix/description/ 2. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后 ...

  9. LeetCode: 56. Merge Intervals(Medium)

    1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...

随机推荐

  1. *Amazon problem: 234. Palindrome Linked List (reverse the linked list with n time)

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  2. CRUD全栈式编程架构之数据层的设计

    CodeFirst 一直以来我们写应用的时候首先都是创建数据库 终于在orm支持codefirst之后,我们可以先建模. 通过模型去创建数据库,并且基于codefirst可以实现方便的 实现数据库迁移 ...

  3. IOS 照片浏览器总结(思想步骤)

    1. 界面分析========================================1> 需要读取或修改属性的控件需要设置属性// 序号标签// 图片// 图片描述// 左边按钮// ...

  4. Android(java)学习笔记45:深入分析Java ClassLoader原理

    1. 前言: Android中的动态加载机制能更好的优化我们的应用,同时实现动态的更新,这就便于我们管理我们的应用,通过插件化来减轻我们的内存以及CPU消耗,在不发布新版本的情况下能更新某些模块. 当 ...

  5. 【甘道夫】NN HA 对于 Client 透明的实验

    之前转载过一篇[伊利丹]写的NN HA实验记录.该博客描写叙述了主备NN透明切换的过程,也就是说,当主NN挂掉后,自己主动将备NN切换为主NN了,Hadoop集群正常执行. 今天我继续做了一个实验.目 ...

  6. 2018.12.25 Spring中JDBCTemplate模版API学习

    1 Spring整合JDBC模版 1.1 spring中土拱了一个可以操作数据库的对象.对象封装了jdbc技术 JDBCTemplateJDBC模板对象 1.2 与DBUtils中的QueryRunn ...

  7. mybatis学习记录六——一对一、一对多和多对多查询

    9       订单商品数据模型 9.1     数据模型分析思路 1.每张表记录的数据内容 分模块对每张表记录的内容进行熟悉,相当 于你学习系统 需求(功能)的过程. 2.每张表重要的字段设置 非空 ...

  8. 如何学好Spring

    要学好Spring,首先要明确Spring是个什么东西,能帮我们做些什么事情,知道了这些然后做个简单的例子,这样就基本知道怎么使用Spring了.Spring核心是IoC容器,所以一定要透彻理解什么是 ...

  9. ListItem Updating事件监视有没有上传附件

    using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; using ...

  10. Xcode 9.0 报错, Safe Area Layout Guide Before IOS 9.0

    Xcode 9.0 新建工程报错 xcode Safe Area Layout Guide Before IOS 9.0 如下图,在Builds for 选择iOS9.0 and Later,不勾选U ...