[leetcode]_Container With Most Water
题目:在二维坐标系下,有很多个挡板,有两个挡板之间能够积蓄的水的最大面积。如下图所示:

思路:我只想到暴力解法,用O(n2)的时间复杂度算出任意两个挡板形成的面积,这必须的过不了。
优化解法:O(n).
用两个指针 i 和 j 指向整个height[]数组的头尾。
if i 指向的高度 < j 指向的高度 , then 用 i 指向的高度 * i , j之间的距离 求的面积,i 指针向后移。
(i指针向后移的原因:因为当前的面积由i 的高度决定,相当于现在获得以i指针为一边挡板的最大面积,因为随着j的向内移动,水平宽度肯定是降低的,然后及时j的高度高于i的高度,也因为矩形面积由短板边<i的高度>决定,因此不会比现在的面积更大。)
同理如果 j 指向的高度 < i 指向的高度, 将j指针向内移动。
代码:
public int maxArea(int[] height) {
int i = 0 , j = height.length - 1;
int max = 0 ;
while(i < j){
int tempArea = 0;
if(height[i] < height[j]){
tempArea = height[i]*(j - i);
i++;
}else{
tempArea = height[j]*(j - i);
j--;
}
if(tempArea > max) max = tempArea;
}
return max;
}
这种精妙的算法,代码很简单,思路很难想到。即使知道了正确解法,还是觉得心有不安。待下次遇见类似问题再深化思考吧。
[leetcode]_Container With Most Water的更多相关文章
- leetcode#42 Trapping rain water的五种解法详解
leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...
- [array] leetcode - 42. Trapping Rain Water - Hard
leetcode - 42. Trapping Rain Water - Hard descrition Given n non-negative integers representing an e ...
- LeetCode 42. Trapping Rain Water 【两种解法】(python排序遍历,C++ STL map存索引,时间复杂度O(nlogn))
LeetCode 42. Trapping Rain Water Python解法 解题思路: 本思路需找到最高点左右遍历,时间复杂度O(nlogn),以下为向左遍历的过程. 将每一个点的高度和索引存 ...
- [LeetCode] 407. Trapping Rain Water II 收集雨水 II
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- [LeetCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- [LeetCode]Container With Most Water, 解题报告
前言 难怪LeetCode OJ在找工作时被很多人推荐,发现了这道最大蓄水题目就是美团的笔试最后一道题,当时我霸笔只有着一道题目没有答出来,因此也就没有获得面试机会,可惜了 题目 Given n no ...
- [leetcode][042] Trapping Rain Water (Java)
我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目在这里: ...
- [LeetCode] Swim in Rising Water 在上升的水中游泳
On an N x N grid, each square grid[i][j] represents the elevation at that point (i,j). Now rain star ...
- C++LeetCode:: Container With Most Water
本来写的题目不是这个,而是字符串匹配,考虑了很多情况写了很久最后看了solution,发现可以用动态规划做.感觉被打击到了,果断先放着重新写一个题,后面心情好了再重新写吧,难过.每天都要被LeetCo ...
随机推荐
- struts (三)
1. <action name="test" class="com.gc.Test"> <result name="success& ...
- JAVA中集合输出的四种方式
在JAVA中Collection输出有四种方式,分别如下: 一) Iterator输出. 该方式适用于Collection的所有子类. public class Hello { public stat ...
- stat 查看文件修改时间
Ø 访问时间(accesstime):读取一次文件的内容,该时间便会更新 Ø 修改时间(modifytime):对文件内容修改一次便会更新该时间. Ø 改变时间(changetime):更改文件 ...
- 微信JSSDK示例代码 笔记
using System; using System.Collections.Generic; using System.Web; using System.IO; using System.Secu ...
- win10 TortoiseSVN 部分图标不显示
原因:https://msdn.microsoft.com/en-us/library/cc144123(VS.85).aspx Note The number of different icon ...
- A+B问题通解_Pascal_C++_Java
世界不断发展,各种电子设备不断变得更加迷你,代码却越写越长…… A+B Problem Input:Two integer A,B Output:The ans of A+B 1971年,Niklau ...
- OpenGL ES为缓存提供数据的7个步骤
OpenGL ES为缓存提供数据的7个步骤: 1.生成glGenBuffers()——请求OpenGL ES为图形处理器控制的缓存生成一个独一无二的标识符. 2.绑定glBindBuffer()——告 ...
- 游戏设计模式系列(一)—— 单线逻辑&&数据驱动,搞定最容易卡死的结算界面
从事游戏行业1年多了,个中心酸不知从何说起.抛开非技术的不说,一个开发者需要面对的最大问题,可能就是和策划频繁改变的需求做斗争了吧,这时候就体现了设计模式的重要性,抛开正式的设计方式不说,先讲讲我1年 ...
- C/C++程序基础
作为一名cocos2dx前端(客户端)开发,知乎上看到一片文章,怎么看待做手游cocos前端开发,lua用的多,c++用的少面试会被鄙视? 为了不被鄙视,所以要学好C++,多做积累.本文主要是根据&l ...
- Orchard官方文档翻译(一) 总览
原文地址:http://docs.orchardproject.net/ 最近想要学习了解orchard,但却没有找到相关的中文文档,只有英文文档.于是决定自行翻译,以便日后方便翻阅. 转载请注明原作 ...