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.

Example

Given [1,3,2], the max area of the container is 2.

Note

You may not slant the container.

从数组两头遍历,计算面积,然后从小的那一段a开始找到下一个比a大的元素。直到所有数组都被遍历。

 public class Solution {
/**
* @param heights: an array of integers
* @return: an integer
*/
public int maxArea(int[] heights) {
int l = heights.length;
if (l == 0) {
return 0;
}
int max = -1;
int i = 0;
int j = l-1;
while (i < j) {
int v = (j - i) * Math.min(heights[i],heights[j]);
if (max < v) {
max = v;
} if(heights[i] > heights[j]){
int x = heights[j];
while (heights[j] <= x && i < j) j--;
}else {
int x = heights[i];
while(heights[i] <= x && i < j) i++;
}
}
return max;
}
}

Container With Most Water(LintCode)的更多相关文章

  1. 【leetcode】Container With Most Water(middle)

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

  2. #LeetCode# Container With Most Water (todo)

    描述: 实现1 -- 求所有可能的值,O(N^2),超时了(因为超时没有跑所有的测试用例,所以不确定还有没有其他问题) 代码: def maxArea(self, height): tmp = len ...

  3. LeetCode第[11]题(Java):Container With Most Water (数组容器盛水)——Medium

    题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...

  4. 在Azure Container Service创建Kubernetes(k8s)群集运行ASP.NET Core跨平台应用程序

    引子 在此前的一篇文章中,我介绍了如何在本地docker环境中运行ASP.NET Core跨平台应用程序(http://www.cnblogs.com/chenxizhang/p/7148657.ht ...

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

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

  6. 22.Container With Most Water(能装最多水的容器)

    Level:   Medium 题目描述: Given n non-negative integers a1, a2, ..., an , where each represents a point ...

  7. LeetCode 11 Container With Most Water(分支​判断问题)

    题目链接 https://leetcode.com/problems/container-with-most-water/?tab=Description   Problem: 已知n条垂直于x轴的线 ...

  8. 11. Container With Most Water(头尾双指针)

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

  9. Implement Trie(LintCode)

    Implement Trie Implement a trie with insert, search, and startsWith methods. 样例   注意 You may assume ...

随机推荐

  1. 三星 C7恢复 出厂设置

    http://jingyan.baidu.com/article/c14654134f0fd20bfcfc4c1e.html

  2. [CodeChef - GERALD07 ] Chef and Graph Queries

    Read problems statements in Mandarin Chineseand Russian. Problem Statement Chef has a undirected gra ...

  3. UIToolBar的半透明属性设置

      UIToolBar的半透明属性设置style:Translucent(Ps:长得很像翻译translation)   https://www.evernote.com/shard/s227/sh/ ...

  4. HDU 1234 开门人和关门人 (模拟)

    题目链接 Problem Description 每天第一个到机房的人要把门打开,最后一个离开的人要把门关好.现有一堆杂乱的机房签  到.签离记录,请根据记录找出当天开门和关门的人.    Input ...

  5. sass_sass安装

    你会不会因为有些事遇到各种各样的问题而搁置,直到把这个事情被耽误了几天.最近一直在弄sass这个东西,安装的过程中各种问题.sass是一个基于ruby环境开发的,安装sass之前得先把ruby给安装了 ...

  6. Ribbon的主要组件与工作流程

    一:Ribbon是什么? Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起.Ribbon客户端组件提供一系列完善的配置项如连接 ...

  7. Linux简介——(一)

    1. 常见操作系统 - 服务端操作系统 : linux.unix.windows server - 单机操作系统 : windows(dos .ucdos.win95.win98.win2000.xp ...

  8. 金融分析二:Matplotlib模板

    Matplotlib:绘图和可视化 Matplotlib是一个强大的Python绘图和数据可视化的工具包. 安装方法:pip install matplotlib 引用方法:import matplo ...

  9. 2017多校第7场 HDU 6127 Hard challenge 极角排序,双指针

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6127 题意:平面直角坐标系上有n个整点,第i个点有一个点权val​,坐标为(xi,yi),其中不存在任 ...

  10. [How to]使用自定义cell进行tableview的创建,适用于cell样式不发生变化的情况。

    1.简介 在tableview中又默认的cell格式,其中组织如下: <截取自官网文档> 最终的在页面上默认的cell也只能像上述那样的显示效果,如果这种要是无法满足我们的界面要求,那么我 ...